1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
-- require "luarocks.require"
md5=require "md5"
lfs=require "lfs"
socket=require "socket"
home = os.getenv( "HOME" )
os.execute("mkdir -p " .. home .. "/.conky/requests");
os.execute("mkdir -p " .. home .. "/.conky/info");
c_timer_init = -1
do
function conky_eval(...)
local arg = table.pack(...)
return conky_parse(table.concat(arg, " "))
end
function conky_multiply_line(times, ...)
local arg = table.pack(...)
local str = table.concat(arg, " ")
local tbl = {}
for i=1,times,1 do
tbl[i] = string.gsub(str,"@i@", i-1)
end
local res = table.concat(tbl,"\n")
return res;
end
function split(delimiter, text)
local list = {}
local pos = 1
if string.find("", delimiter, 1) then -- this would result in endless loops
error("delimiter matches empty string!")
end
while 1 do
local first, last = string.find(text, delimiter, pos)
if first then -- found?
table.insert(list, string.sub(text, pos, first-1))
pos = last+1
else
table.insert(list, string.sub(text, pos))
break
end
end
return list
end
function conky_filter_real_ip(iface)
local req = string.format('${addrs %s}', iface)
local ips = conky_parse(req);
--[[
print(ips)
--]]
local list = split(",", ips)
for i, item in ipairs(list) do
if (string.match(item, "141%.52%.64")) then
return item
end
end
return "Unknown"
end
user = split('/', home)
user = user[#user]
result_check_server_status = {}
result_check_adei_source = {}
result_check_server_ = {}
result_check_service_ = {}
online = {}
status = {}
tested = {}
c_timer = c_timer_init
width = 80
update_time = 0
outdate_time = 0
timeout = 2
function conky_set_width (w)
width = tonumber(w)
return ""
end
function conky_set_timeout (t)
timeout = tonumber(t)
return ""
end
function cmd_popen(server, port, cmd)
if (c_timer_init == 0) then
return io.popen(cmd)
else
local cmd_md5 = md5.sumhexa(cmd)
local fn = string.format("%s/.conky/info/%s:%s-%s.srv", home, server, port, cmd_md5)
stat = lfs.attributes(fn)
if (stat == nil) then
return nil
end
local since = os.difftime(stat['modification'], update_time)
if (since < 0) then
return nil
end
since = os.difftime(outdate_time, os.time())
if (since < 0) then
return nil
end
return io.open(fn, "r")
end
end
function conky_check_server_status(server, port, ...)
port = port or 22
local arg = table.pack(...)
local name = string.format("%s:%i", server, port);
if (c_timer < 0) then
if (c_timer == -1) then
local fn = string.format("%s/.conky/requests/%s:%i.srv", home, server, port)
tested[string.format("%s:%i", server, port)] = false;
local f = assert(io.open(fn, "w"))
local cmd = string.format("/etc/conky/scripts/ping.pl %s:%i %i", server, port, timeout)
f:write(cmd .. "\n")
if (arg[1] ~= "-") then
cmd = string.format("ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_server_status.sh 2>&1 | fold -w %i", port, timeout, server, width)
f:write(cmd .. "\n")
for i, service in ipairs(arg)
do
cmd = string.format("ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_%s_status.sh 2>&1 | fold -w %i", port, timeout, server, service, width)
f:write(cmd .. "\n")
end
end
f:close()
end
if (result_check_server_status[name] == nil) then
local res
if (math.abs(c_timer)%2 > 0) then
res = "${color yellow}?${color}"
else
res = "${color yellow}.${color}"
end
if (arg[1] ~= "-") then
res = res .. " "
for i, service in ipairs(arg) do
res = res .. " "
end
end
--result_check_server_status[server] = res
--status[server] = false
return res
end
elseif (c_timer == 0) then
result_check_server_status[name] = check_server_status(server, port, arg)
end
return (result_check_server_status[name] == nil) and "" or result_check_server_status[name]
end
function check_server_status(server, port, ...)
port = port or 22
local arg = table.pack(...)
local additional
if arg ~= nil then
additional = arg[1]
end
local res
local cmd = string.format("/etc/conky/scripts/ping.pl %s:%i %i", server, port, timeout)
local cmdf = cmd_popen(server, port, cmd)
if (cmdf == nil) then
res = -1
else
local data = cmdf:read("*a")
res = (data == nil) and -1 or tonumber(data)
cmdf:close()
end
if (additional[1] == "-") then
if (res > 0) then
online[server] = true
return "${color green}*"
elseif (res < 0) then
return "${color red}?"
else
online[server] = false
return "${color red}*"
end
end
if res == nil then
res = -1
end
if (res > 0) then
online[server] = true
local output
cmd = string.format("ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_server_status.sh 2>&1 | fold -w %i", port, timeout, server, width)
cmdf = cmd_popen(server, port, cmd)
if (cmdf == nil) then
output = string.format("${color green}* ${color red}?")
else
local res = {}
local line = cmdf:read("*l")
while line do
local m1 = string.match(line, "^(.*[^%s])%s*$")
if (m1) then
table.insert(res, m1)
end
line = cmdf:read("*l")
end
-- res = cmdf:read("*a")
cmdf:close()
res = table.concat(res, "\n")
if (string.match(res, "[%a%d]")) then
table.insert(status, string.format("${color yellow}%s:%i${color gray}\n%s", server, port, res))
output = string.format("${color green}* ${color red}*")
else
output = string.format("${color green}* *")
end
end
for i, service in ipairs(additional)
do
cmd = string.format("ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_%s_status.sh 2>&1 | fold -w %i", port, timeout, server, service, width)
cmdf = cmd_popen(server, port, cmd)
if (cmdf == nil) then
output = output .. string.format(" ${color red}?")
else
local res = {}
local line = cmdf:read("*l")
while line do
local m1 = string.match(line, "^(.*[^%s])%s*$")
if (m1) then
table.insert(res, m1)
end
line = cmdf:read("*l")
end
cmdf:close()
if (res[1]) then
local info = table.remove(res)
local s = 0
local extra = ""
local m1, m2 = string.match(info, "^(%d+)(%s.+)$")
if (m1 == nil) then
m1 = string.match(info, "^(%d+)");
end
if (m1 == nil) then
table.insert(res, info)
else
s = tonumber(m1)
if (m2 ~= nil) then
extra = string.sub(m2,2)
end
end
if (res[1]) then
res = table.concat(res, "\n")
table.insert(status, string.format("${color yellow}%s:%i:%s${color gray}\n%s", server, port, service, res))
end
if (s == 0) then
output = output .. string.format(" ${color red}*")
elseif (s == 1) then
output = output .. string.format(" ${color green}*")
else
output = output .. string.format(" ${color yellow}*")
end
output=output .. " ${color white}" .. extra;
else
output = output .. string.format(" ${color green}*")
end
end
end
return output
else
if (res < 0) then
res = string.format("${color red}? ")
else
online[server] = false
res = string.format("${color red}* ")
end
if (additional ~= nil) then
for i, service in ipairs(additional) do
res = res .. " "
end
end
return res
end
end
function conky_check_adei_source(server, port, config, setup, db_server, db_name, ignore_list)
port = port or 22
local name = string.format("%s:%i__%s__%s", server, port, db_server, db_name)
if (c_timer < 0) then
if (c_timer == -1) then
ignore_list = ignore_list or ""
local fn = string.format("%s/.conky/requests/%s:%i.srv", home, server, port)
local f = assert(io.open(fn, "a+"))
local cmd = string.format('ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_adei_source.sh %s %s %s %s "%s" 2>&1', port, timeout, server, config, setup, db_server, db_name, ignore_list)
f:write(cmd .. "\n")
f:close()
end
if (result_check_adei_source[name] == nil) then
return "${color yellow}?${color}"
else
if (online[server]) then
return result_check_adei_source[name];
else
return "${color red}?${color}"
end
end
elseif (online[server]) then
if (c_timer == 0) then
result_check_adei_source[name] = check_adei_source(server, port, config, setup, db_server, db_name, ignore_list)
end
return result_check_adei_source[name];
else
return "${color red}?${color}"
end
end
function check_adei_source(server, port, config, setup, db_server, db_name, ignore_list)
ignore_list = ignore_list or ""
cmd = string.format('ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_adei_source.sh %s %s %s %s "%s" 2>&1', port, timeout, server, config, setup, db_server, db_name, ignore_list)
cmdf = cmd_popen(server, port, cmd)
if (cmdf == nil) then
return "${color red}?"
end
local res = {}
line = cmdf:read("*l")
while line do
local m1 = string.match(line, "^(.*[^%s])%s*$")
if (m1) then
table.insert(res, m1)
end
line = cmdf:read("*l")
end
cmdf:close()
local info = table.remove(res)
local t1, t2, t3 = string.match(info, "^(%d+)%s+(%d+)%s+(%d+)")
local s = tonumber(t1)
if (s == nil) then
table.insert(res, info)
s = 0
end
res = table.concat(res, "\n")
local output
if (s == 1) then
output="${color green}*"
else
--[[
table.insert(status, string.format("${color yellow}%s -- %s${color gray}\n%s\n", db_server, db_name, res))
--]]
if (s == 0) then output="${color red}*"
else output="${color yellow}*" end
end
-- if (s ~= 0) then
local groups = tonumber(t2)
if (groups) then
output = output .. string.format("${color white} %i groups", groups);
else
output = output .. string.format("${color red} ? groups");
end
local size = tonumber(t3)
if (groups and size) then
output = output .. string.format("${color white}, %i GB", size);
else
output = output .. string.format("${color red}, ? GB");
end
-- end
if (string.match(res, "[%a%d]")) then
output = output .. "${color gray}\n ${font Bitstream Vera Sans Mono:size=7}" .. string.gsub(res,"\n", "${font}\n ${font Bitstream Vera Sans Mono:size=7}") .. "${font}${color white}"
end
return output
end
function conky_check_server_(service, server, port, ...)
port = port or 22
local arg = table.pack(...)
local name = string.format("%s:%i:%s", server, port, service)
if (c_timer < 0) then
if (c_timer == -1) then
ignore_list = ignore_list or ""
local fn = string.format("%s/.conky/requests/%s:%i.srv", home, server, port)
local f = assert(io.open(fn, "a+"))
local cmd = string.format("ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_server_%s.sh 2>&1", port, timeout, server, service)
f:write(cmd .. "\n")
f:close()
end
if (result_check_server_[name] == nil) then
return "${color yellow}?${color}"
else
if (online[server]) then
return result_check_server_[name];
else
return "${color red}?${color}"
end
end
elseif (online[server]) then
if (c_timer == 0) then
result_check_server_[name] = check_server_(service, server, port, arg)
end
return result_check_server_[name];
else
return "${color red}?${color}"
end
end
function conky_check_service_(service, id, ...)
local arg = table.pack(...)
local name = string.format("%s:%s", service, id)
if (c_timer < 0) then
if (c_timer == -1) then
ignore_list = ignore_list or ""
tested[string.format("%s:%s", service, id)] = false;
local fn = string.format("%s/.conky/requests/%s:%s.srv", home, service, id)
local f = assert(io.open(fn, "a+"))
local cmd = string.format("/etc/conky/service/check_%s.sh %s %s 2>&1", service, id, table.concat(arg, " "))
f:write(cmd .. "\n")
f:close()
end
if (result_check_server_[name] == nil) then
return "${color yellow}?${color}"
else
return result_check_server_[name];
end
else
if (c_timer == 0) then
result_check_server_[name] = check_service_(service, id, arg)
end
return result_check_server_[name];
end
end
function format_traffic(value, yellow, red)
yellow = yellow or 100
red = red or 1000
local value = math.floor(tonumber(value) / 1073741824)
local res
if (value > red) then res="${color red}"
elseif (value > yellow) then res="${color yellow}"
else res="${color white}" end
if (value < 10) then res = res .. " " .. value
elseif (value < 100) then res = res .. " " .. value
else res = res .. value end
return res .. " GB${color gray}"
end
function check_server_(service, server, port, opts)
port = port or 22
local cmd = string.format("ssh -x -p %i -o ConnectTimeout=%i root@%s /opt/scripts/check_server_%s.sh 2>&1", port, timeout, server, service)
cmdf = cmd_popen(server, port, cmd)
if (cmdf == nil) then
return "${color red}?"
end
local res = {}
line = cmdf:read("*l")
if (line == nil) then
-- we can try another iteration?
socket.select(nil, nil, timeout)
line = cmdf:read("*l")
end
while line do
local m1 = string.match(line, "^(.*[^%s])%s*$")
if (m1) then
table.insert(res, m1)
end
line = cmdf:read("*l")
end
cmdf:close()
if (service == "traffic") then
if (res[1]) then
local t1, t2, t3 = string.match(res[1], "^(%d+)%s+(%d+)%s+(%d+)")
if (t1 and t2 and t3) then
if ((opts) and (opts[1])) then yellow = tonumber(opts[1])
else yellow = nil end
if ((opts) and (opts[2])) then red = tonumber(opts[2])
else red = nil end
t1=format_traffic(t1,yellow,red)
t2=format_traffic(t2,yellow,red)
t3=format_traffic(t3,yellow,red)
return string.format("${color gray}i: %s, o: %s, f: %s", t1, t2, t3)
else
return "${color red}" .. res[1]
end
else
return "${color red}?"
end
end
return ""
end
function check_service_(service, id, opts)
local optlist = ((opts == nil) and "" or table.concat(opts, " "))
local cmd = string.format("/etc/conky/service/check_%s.sh %s %s 2>&1", service, id, optlist)
cmdf = cmd_popen(service, id, cmd)
if (cmdf == nil) then
return "${color red}?"
end
local res = {}
local local_res = {}
line = cmdf:read("*l")
while line do
local t, m1 = string.match(line, "^(%*?)(.*[^%s])%s*$")
if (m1) then
if (t == nil) or (t == '') then
table.insert(res, m1)
else
table.insert(local_res, string.sub(m1,1))
end
end
line = cmdf:read("*l")
end
cmdf:close()
local output
local info = table.remove(res)
local t1 = 0
local t2 = nil
local msg = ''
if (info) then
t1, t2, msg = string.match(info, "^(%d+)%s+(%d+)(.*)")
end
local s = tonumber(t1)
if (s == nil) then
table.insert(res, info)
s = 0
end
if (s == 1) then
output="${color green}*"
else
if (s == 0) then output="${color red}*"
else output="${color yellow}*" end
end
s = tonumber(t2)
if (s == nil) then
s = 0
else
if (s == 1) then
output=output .. " ${color green}*"
else
if (s == 0) then output=output .. " ${color red}*"
else output=output .. " ${color yellow}*" end
end
end
output = output .. " ${color white}" .. msg
res = table.concat(res, "\n")
local_res = table.concat(local_res, "\n")
if (string.match(res, "[%a%d]")) then
table.insert(status, string.format("${color yellow}%s:%s${color gray}\n%s", service, id, res))
end
if (string.match(local_res, "[%a%d]")) then
output = output .. "${color gray}\n ${font Bitstream Vera Sans Mono:size=7}" .. string.gsub(local_res,"\n", "${font}\n ${font Bitstream Vera Sans Mono:size=7}") .. "${font}${color white}"
end
return output
end
function conky_print_server_errors(interval, tag)
local res = table.concat(status, "\n")
if (c_timer < 0) then
tag = user .. "_" .. tag
if (c_timer == -1) then
update_time = os.time()
local result = 0;
while (result == 0) do
result = os.execute("ps x | grep conky_process_requests | grep " .. tag .. " | grep -v grep > /dev/null")
end
for item, s in pairs(tested) do
os.execute("/etc/conky/scripts/conky_process_requests.pl \"" .. item .. ".srv\" " .. tag .. " &")
end
end
local result=os.execute("ps x | grep conky_process_requests | grep " .. tag .. " | grep -v grep > /dev/null")
if (result == 0) then
c_timer = c_timer - 1;
local since = os.difftime(os.time(), update_time)
if (since > tonumber(interval)) then
result_check_server_status = {}
result_check_adei_source = {}
result_check_server_ = {}
end
else
outdate_time = os.time() + 2 * tonumber(interval)
c_timer = 0
end
return ""
else
c_timer = c_timer + conky_info.update_interval
if (c_timer > tonumber(interval)) then
status = {}
tested = {}
c_timer = c_timer_init
end
end
if (string.match(res, "[%a%d]")) then
return string.format("\n\n%s", res)
else
return ""
end
end
function conky_outcon(pos1, pos2)
local n = tonumber(conky_parse('${tcp_portmon 32768 65535 count}'))
local res = {}
local str = ""
for i=1,n do
local val = tostring(conky_parse(string.format('${tcp_portmon 32768 65535 rhost %i}:${tcp_portmon 32768 65535 rport %i}',i - 1,i - 1)))
if (res[val] == nil) then
res[val] = 1
else
res[val] = res[val] + 1
end
end
local names = {}
for addr,num in pairs(res) do
table.insert(names, addr)
end
table.sort(names, function(a,b) return res[a]>res[b] end)
for i=1,#names do
local name = names[i]
str = str .. string.format("${goto %i}%s${goto %i}cnt %3i\n",pos1,name,pos2,res[name])
end
return str
end
end
|