This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrc.py
executable file
·1078 lines (703 loc) · 33.4 KB
/
rrc.py
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#needs pip install schedule
import json
import sys
import threading
import os
import subprocess
import signal
import commands
import collections
import logging
from or_class import *
import MySQLdb
import argparse
import fnmatch
import syslog
#Global definitions
global configfile
global databasefile
global ctrl_obj
global probe_obj
global database_obj
global startuplist
global msg
global controllercounter
global probecounter
global dbcounter
global dbcondition
#controller
ctrl_obj = []
probe_obj = []
database_obj = []
startuplist = {}
dirtmp="/tmp/"
dbcondition=0
# progressbar
def progressbar(count, total, suffix=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s -->%s\r' % (bar, percents, '%', suffix))
sys.stdout.flush() # As suggested by Rom Ruben
def cls():
os.system('cls' if os.name=='nt' else 'clear')
def banner():
print " ___ _ ___ _ _ ___ _ _ _ "
print "| _ \__ _ __| |__ _ _ _ ___| _ \___ __ _ __| |_(_)_ _____ ___ / __|___ _ _| |_ _ _ ___| | |___ _ _"
print "| / _` / _` / _` | '_|___| / -_) _` / _| _| \ V / -_)___| (__/ _ \ ' \ _| '_/ _ \ | / -_) '_|"
print "|_|_\__,_\__,_\__,_|_| |_|_\___\__,_\__|\__|_|\_/\___| \___\___/_||_\__|_| \___/_|_\___|_|"
# loadconfig
def loadconfig( jsonfile ):
#In-ram configuration
global inram_configuration
try:
with open(jsonfile) as data_file:
inram_configuration = json.load(data_file)
except IOError as e:
print "\nno go:We got a probelm..I'm looking for "+jsonfile+" but I can't find th file for please check.\n"
except:
print "\nno go:The "+jsonfile+" file is not correct , please check the syntax and/or file logic\n"
raise
return True
def checkduplicatescontroller(cid):
d=0
for k in range(0,controllercounter):
if cid == ctrl_obj[k].controllername_id:
d+=1
if d > 1:
print "\n\nnogo: The controllername_id must be unique , I have found some duplicates on "+ctrl_obj[k].controllername_id
return True
k +=1
def checkduplicatesprobe(pid):
d=0
for k in range(0,probecounter):
if pid == probe_obj[k].probename_id:
d+=1
if d > 1:
print "\n\nnogo: The probename_id must be unique , I have found some duplicates on "+probe_obj[k].probename_id
return True
k +=1
def checkduplicatesdatabase(did):
d=0
for k in range(0,dbcounter):
if did == database_obj[k].database_id:
d+=1
if d > 1:
print "\n\nnogo: The database_id must be unique , I have found some duplicates on "+database_obj[k].database_id
return True
k +=1
# buildobcject
def objectbuilder():
global controllercounter
global probecounter
controllercounter = 0
probecounter = 0
abs_probeindex=0
lastresult=""
try:
#check for controller
for row_ctrl in inram_configuration['controllerset']:
#Creating controller object totalprobes will be update leater now is 1
#Setting total number of probe
totalprobes=json.dumps(row_ctrl['probeset']).count("probename_id")
ctrl_obj.append(Controller(
row_ctrl['controllername_id'],
row_ctrl['condition'],
row_ctrl['expected_value'],
parsecommand(row_ctrl['ifsatisfied_action']),
parsecommand(row_ctrl['ifnotsatisfied_action']),
row_ctrl['repeat_ifsatisfied_action'],
row_ctrl['repeat_ifnotsatisfied_action'],
row_ctrl['rearm_after'],
parsecommand(row_ctrl['rearm_action']),
totalprobes,controllercounter,lastresult))
controllercounter +=1
pbindex=0
lastresult=""
for pbindex in range(0,totalprobes):
probe_obj.append(Probe(
row_ctrl['probeset'][pbindex]['probename_id'],
row_ctrl['probeset'][pbindex]['sql'],
row_ctrl['probeset'][pbindex]['sqlengine'],
parsecommand(row_ctrl['probeset'][pbindex]['probefile']),
controllercounter-1,pbindex,abs_probeindex,lastresult))
pbindex +=1
probecounter +=1
abs_probeindex +=1
#Go to the next probe
#Public value
return True
except KeyError:
print "\n\nnogo: The configuration file "+configfile+" seem has logical corrupted prbably some fields are disappeared\n"
except:
print "\n\nnogo: oops some problem occured during validation checking process\n"
raise
return True
def dbobjectbuilder():
global dbcounter
dbcounter = 0
try:
for row_ctrl in inram_configuration['databases']:
database_obj.append(Dbengine(
row_ctrl['database_id'],
row_ctrl['dbengine'],
row_ctrl['host'],
row_ctrl['database'],
row_ctrl['username'],
row_ctrl['password']
,dbcounter))
dbcounter +=1
return True
except KeyError:
print "\n\nnogo: The configuration file "+databasefile+" seem has logical corrupted prbably some fields are disappeared\n"
except:
print "\n\nnogo: oops some problem occured during validation checking process\n"
raise
return True
def chkrange(c,val,min,max,attribute):
if (val >= min) and (val <= max) :
return True
else:
print "\n\nnogo: The "+attribute+"is set to "+str(val)+" on "+ctrl_obj[c].controllername_id+" but must be => "+str(min)+" and <= "+str(max)+" secons\n"
return False
def chkoptions(c,val,options,attribute):
if "null" in options and val == "":
return True
else:
if (val in options) and val:
return True
else:
print "\n\nnogo: The "+attribute+" needs to be "+options+"\n"
return False
def chkfile(filename,section):
#ignoring paramenters
filename =filename.split(' ',1)[0].replace("\"","")
if ((os.path.exists(filename) and os.access(filename, os.X_OK)) and (filename.find("/") != -1)) or filename =="" :
return True
else:
print "\n\nnogo: The file named "+filename+" definend on "+section+" must meet three requirements:"
print "nogo: Existent executable and needs absolute path\n"
return False
def checklogiccontroller(c):
if not (ctrl_obj[c].controllername_id):
print "\n\nnogo: The controllername_id must be set"
return False
else:
#Check for dup licates
if checkduplicatescontroller(ctrl_obj[c].controllername_id) == True:
return False
else:
if (ctrl_obj[c].condition) == False:
print "\nno go: All conditions must be set"
return False
else:
if (ctrl_obj[c].expected_value) == False:
print "\nno go: All expected_value must be set"
return False
else:
if chkfile(ctrl_obj[c].ifsatisfied_action,ctrl_obj[c].controllername_id) == False:
return False
else:
if chkfile(ctrl_obj[c].ifnotsatisfied_action,ctrl_obj[c].controllername_id) == False:
return False
else:
if chkrange(c,ctrl_obj[c].rearm_after,0,31536000,'Rearm') == False:
return False
else:
if chkfile(ctrl_obj[c].rearm_action,ctrl_obj[c].controllername_id) == False:
return False
else:
if chkfile(ctrl_obj[c].rearm_action,ctrl_obj[c].controllername_id) == False:
return False
else:
options="once,ever,null"
if ctrl_obj[c].ifsatisfied_action and chkoptions(c,ctrl_obj[c].repeat_ifsatisfied_action,options,'repeat_ifsatisfied_action') == False:
return False
else:
if ctrl_obj[c].ifnotsatisfied_action and chkoptions(c,ctrl_obj[c].repeat_ifnotsatisfied_action,options,'repeat_ifnotsatisfied_action') == False:
return False
else:
return True
def checklogicprobe(i):
if not (probe_obj[i].probename_id):
print "\n\nnogo: The probename_id must be set"
return False
else:
if checkduplicatesprobe(probe_obj[i].probename_id) == True:
return False
else:
if (probe_obj[i].sql,probe_obj[i].probename_id) == False:
return False
else:
if (probe_obj[i].sql) and (probe_obj[i].probefile):
print "\n\nno go:Can not use dirctive sql and probefile togheter on "+probe_obj[i].probename_id;
print
return False
else:
if (probe_obj[i].sql) == False :
print"\n\n"
print "Sql experssion must be set on "+probe_obj[i].probename_id;
print
return False
else:
if (probe_obj[i].sql) and not (probe_obj[i].sqlengine) :
print"\n\n"
print "Sql engine must be set on "+probe_obj[i].probename_id;
print
return False
else:
if chkfile(probe_obj[i].probefile,probe_obj[i].probename_id) == False :
return False
else:
i +=1
return True
def checklogicdatabase(i):
if not(database_obj[i].database_id):
print "\n\nnogo: The database_id must be set"
return False
else:
if checkduplicatesdatabase(database_obj[i].database_id) == True:
return False
else:
options="mysql,postgres,oracle,mssql"
if chkoptions(i,database_obj[i].dbengine,options,'dbengine') == False:
return False
else:
if database_obj[i].dbengine == "mysql":
if not(database_obj[i].host) or not(database_obj[i].database) or not(database_obj[i].username) or not(database_obj[i].password):
print
print
print"no go:All database paramenters are necessary"
return False
else:
return True
else:
print
print
print"no go:Only mysql is supported ad the moment, we are at work for others please fork me on https://github.com/ottacom/Radar-Reactive-Controller"
return False
def checkdbconnection(index):
try:
db = MySQLdb.connect(host=database_obj[index].host,
user=database_obj[index].username,
passwd=database_obj[index].password,
db=database_obj[index].database)
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
results = cursor.fetchone()
# Check if anything at all is returned
if results:
return True
else:
return False
except MySQLdb.Error:
print"\n\nno go: Connection database problem "+database_obj[index].host+" plese check user password and access to the database"
print
print
return False
def checkzombie(name,max_process):
chkps=commands.getstatusoutput("ps -ef |grep "+name+" |grep -v grep |wc -l")
#chkps=commands.getstatusoutput("ps -ef |grep "+name+"| grep -v grep ")
#Pay attention the list give it back status and arguments
#1 process = process +1
if int(chkps[1]) < max_process+1:
#Found some zombie process
return True
else:
return False
def jobsimulation(job,startprobe):
try:
print "--->Starting Radar Controller simulation for:"+ctrl_obj[job].controllername_id
i=0
for i in range(startprobe, startprobe+ctrl_obj[job].totalprobes):
print "------>Starting probe "+probe_obj[i].probename_id
if (probe_obj[i].sql) :
print "--------->Executing SQL "+probe_obj[i].sql
dbindex=0
while (probe_obj[i].sqlengine == database_obj[dbindex].dbengine):
dbindex+=1
probe_obj[i].lastresult=executesql(probe_obj[i].sql,dbindex)
print "--------->Sql on "+probe_obj[i].probename_id+" has returns "+str(probe_obj[i].lastresult)
#Condition valorizing
ctrl_obj[job].condition=ctrl_obj[job].condition.replace(str(probe_obj[i].probename_id),str(probe_obj[i].lastresult))
else:
print "-------->Executing Command "+probe_obj[i].probefile
#split parameters
probe_obj[i].lastresult=os.system(probe_obj[i].probefile+"> /dev/null")
print "-------->Command on "+probe_obj[i].probename_id+" has returns "+str(probe_obj[i].lastresult)
#Condition valorizing
ctrl_obj[job].condition=ctrl_obj[job].condition.replace(str(probe_obj[i].probename_id),str(probe_obj[i].lastresult))
i =+1
print "--->Verifing conditions "+ctrl_obj[job].condition
ctrl_obj[job].lastresult=executesql(ctrl_obj[job].condition,dbcondition)
if str(ctrl_obj[job].lastresult) == str(ctrl_obj[job].expected_value):
print "--->Condition is satisfied we got "+str(ctrl_obj[job].lastresult)+" and we expected "+str(ctrl_obj[job].expected_value)
if (ctrl_obj[job].ifsatisfied_action):
print "--->This is a simulation and I don't start "+str(ctrl_obj[job].ifsatisfied_action)+",I will do that outside the simulation"
else :
print "--->Condition is not satisfied we got "+str(ctrl_obj[job].lastresult)+" but we expected "+str(ctrl_obj[job].expected_value)
if (ctrl_obj[job].ifnotsatisfied_action):
print "--->This is a simulation and I don't start "+ctrl_obj[job].ifnotsatisfied_action+",I will do that outside the simulation"
#open(dirtmp+, ctrl_obj[job].controllername_id.false).close()
if (ctrl_obj[job].rearm_after):
print "--->Rearm after "+str(ctrl_obj[job].rearm_after)+" times satisfied_action"
if (ctrl_obj[job].rearm_action):
print "--->The rearm is set but this is a simulation and I don't start "+ctrl_obj[job].rearm_action+",I will do that outside the simulation"
print
print
except:
print "\n\n!!!!!!!!!!!!Unrecovable problem occured: Ouch... something is going wrong please check your scripts and sql query"
print
print
def executesql (sql,dbindex):
try:
db = MySQLdb.connect(host=database_obj[dbindex].host,
user=database_obj[dbindex].username,
passwd=database_obj[dbindex].password,
db=database_obj[dbindex].database)
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchone()[0]
# Check if anything at all is returned
if results:
return results
else:
return False
except MySQLdb.Error:
print
print "!!!!!!!!!!!!!"
print "Ouch.. We have a database problem during SQL execution on "+database_obj[dbindex].host+"\nThe sql query \""+sql+"\" has returned an unexpected Error\nplease check sql sintax user password and if you have access to the database"
print
return False
# now, to clear the screen
def simulation():
cls()
banner()
print "\n\nRadar-Reactive-controller go-nogo for syntax check control\n"
#if checkzombie('start.py',1) == False:
# progressbar(0,100,'Check for another instance...... ')
# print "\n\nnogo: Another instace of observium-radar still running something wrong before, please check around and try again\n"
# quit()
#else:
if loadconfig(configfile) == True:
progressbar(5,100,'Load file...... ')
else:
quit()
#Second stage
if objectbuilder() == True:
progressbar(10,100,'Check config........ ')
else:
quit()
#Third stage
i=0
lastbar=10
for i in range(0,controllercounter):
if checklogiccontroller(i) == True:
lastbar=lastbar+20/controllercounter;
progressbar(lastbar+(20/controllercounter)*i,100,'Check logic & relations....... ')
else:
quit()
i +=1
i=0
for i in range(0, probecounter):
if checklogicprobe(i) == True:
lastbar=lastbar+20/probecounter;
progressbar(lastbar+(20/probecounter)*i,100,'Check logic & relations probe....... ')
else:
quit()
i +=1
#Configuring database
if loadconfig(databasefile) == True:
lastbar=lastbar+5;
progressbar(lastbar,100,'Loading database congfig file...... ')
else:
quit()
if dbobjectbuilder() == True:
lastbar=lastbar+5;
progressbar(lastbar+5,100,'Configuring database...... ')
else:
quit()
i=0
for i in range(0,dbcounter):
if checklogicdatabase(i) == True:
lastbar=lastbar+5/dbcounter;
progressbar(lastbar+(20/dbcounter)*i,100,'Check logic database....... ')
else:
quit()
i +=1
i=0
progressbar(lastbar+(20/dbcounter)*i,100,'Check database connectivities....... ')
for i in range(0,dbcounter):
if checkdbconnection(i) == True:
lastbar=lastbar+25/dbcounter;
progressbar(lastbar+(20/dbcounter)*i,100,'Check database connectivities....... ')
else:
quit()
i +=1
progressbar(100,100,'Check complete!!!! ')
print
print
print "Radar Reactive Controller syntax is OK , we are ready to simulating the Radar"
print "\n\n"
i=0
startprobe=0
for i in range(0,controllercounter):
p=0
while (probe_obj[p].controllerindex != i):
p +=1
startprobe=probe_obj[p].abs_probeindex
jobsimulation(i,startprobe)
i +=1
def production(silent):
defaultmsg="Something is going wrong.. please check you configuration using -m simulation"
if silent == False:
logger ("Starting RRC","alert")
if loadconfig(configfile) == True:
logger ("Inizializing....","alert")
else:
logger (defaultmsg,"err")
quit()
if objectbuilder() == True:
logger ("Workflow created..","alert")
else:
logger (defaultmsg,"err")
quit()
if loadconfig(databasefile) == True:
logger ("Inizializing database","alert")
else:
logger (defaultmsg,"err")
quit()
if dbobjectbuilder() == True:
logger ("Database Connection created....","alert")
else:
logger (defaultmsg,"err")
quit()
logger ("RRC has been started","alert")
startprobe=0
for i in range(0,controllercounter):
p=0
while (probe_obj[p].controllerindex != i):
p +=1
startprobe=probe_obj[p].abs_probeindex
jobexecute(i,startprobe)
i +=1
logger ("RRC is terminated","alert")
else:
if loadconfig(configfile) == False:
print defaultmsg
quit()
if objectbuilder() == False:
print defaultmsg
quit()
if loadconfig(databasefile) == False:
print defaultmsg
quit()
if dbobjectbuilder() == False:
print defaultmsg
quit()
startprobe=0
for i in range(0,controllercounter):
p=0
while (probe_obj[p].controllerindex != i):
p +=1
startprobe=probe_obj[p].abs_probeindex
jobexecutesilent(i,startprobe)
i +=1
def jobexecute(job,startprobe):
try:
executefile =""
par=""
tmpfile=ctrl_obj[job].controllername_id.replace(" ", "_")
i=0
for i in range(startprobe, ctrl_obj[job].totalprobes+startprobe):
if (probe_obj[i].sql) :
dbindex=0
while (probe_obj[i].sqlengine == database_obj[dbindex].dbengine):
dbindex+=1
probe_obj[i].lastresult=executesql(probe_obj[i].sql,dbindex)
#Condition valorizing
ctrl_obj[job].condition=ctrl_obj[job].condition.replace(str(probe_obj[i].probename_id),str(probe_obj[i].lastresult))
else:
#probe_obj[i].lastresult=os.system(probe_obj[i].probefile)
probe_obj[i].lastresult=subprocess.check_output(probe_obj[i].probefile, shell=True)
#Condition valorizing
ctrl_obj[job].condition=ctrl_obj[job].condition.replace(str(probe_obj[i].probename_id),str(probe_obj[i].lastresult))
i =+1
ctrl_obj[job].lastresult=executesql(ctrl_obj[job].condition,dbcondition)
#Condition satisfied
if str(ctrl_obj[job].lastresult) == str(ctrl_obj[job].expected_value):
logger("Controller "+ctrl_obj[job].controllername_id+" is satisfied","alert")
if ctrl_obj[job].rearm_after == 0:
if (ctrl_obj[job].ifsatisfied_action) and not(os.path.exists(dirtmp+'OK_'+tmpfile)) and (ctrl_obj[job].repeat_ifsatisfied_action=="once"):
logger ("Controller "+ctrl_obj[job].controllername_id+" has started the ifsatisfied_action in "+ctrl_obj[job].repeat_ifsatisfied_action,"alert")
logger ("Action: "+ctrl_obj[job].ifsatisfied_action,"alert")
os.system(ctrl_obj[job].ifsatisfied_action+"> /dev/null")
if (os.path.exists(dirtmp+'KO_'+tmpfile)):
os.remove(dirtmp+'KO_'+tmpfile)
touch(dirtmp+'OK_'+tmpfile)
if (ctrl_obj[job].ifsatisfied_action) and (ctrl_obj[job].repeat_ifsatisfied_action=="ever"):
logger ("Controller "+ctrl_obj[job].controllername_id+" has started the ifsatisfied_action , in "+ctrl_obj[job].repeat_ifsatisfied_action,"alert")
logger ("Action: "+ctrl_obj[job].ifsatisfied_action,"alert")
os.system(ctrl_obj[job].ifsatisfied_action+" > /dev/null")
if (os.path.exists(dirtmp+'OK_'+tmpfile)):
os.remove(dirtmp+'OK_'+tmpfile)
touch(dirtmp+'KO_'+tmpfile)
else:
#REARM
if str(ctrl_obj[job].lastresult) == str(ctrl_obj[job].expected_value) and ctrl_obj[job].rearm_after > 0 and (os.path.exists(dirtmp+'KO_'+tmpfile)):
totalfile=""
logger ("Rearm after "+str(ctrl_obj[job].rearm_after)+" times satisfied_action","alert")
totalfile=len(fnmatch.filter(os.listdir(dirtmp), '*.'+tmpfile))
tfile=int(totalfile)+1
touch(dirtmp+str(tfile)+'.'+tmpfile)
toleft=ctrl_obj[job].rearm_after-tfile
logger ("Controller "+ctrl_obj[job].controllername_id+" has been rearmed after "+str(ctrl_obj[job].rearm_after)+" times satisfied "+str(toleft)+" left","alert")
if (tfile >=ctrl_obj[job].rearm_after):
os.system(ctrl_obj[job].rearm_action+"> /dev/null")
logger ("Controller "+ctrl_obj[job].controllername_id+" has been rearmed!","alert")
logger ("Controller "+ctrl_obj[job].controllername_id+" has started the rearm action ","alert")
logger ("Action:"+ctrl_obj[job].rearm_action,"notice")
logger ("Controller "+ctrl_obj[job].controllername_id+" has started the ifsatisfied_action in "+ctrl_obj[job].repeat_ifsatisfied_action,"alert")
logger ("Action: "+ctrl_obj[job].ifsatisfied_action,"alert")
files = os.listdir(dirtmp)
for f in files:
if not os.path.isdir(f) and tmpfile in f:
print dirtmp+f
os.remove(dirtmp+f)
touch(dirtmp+'OK_'+tmpfile)
#Condition Unsatisfied
if str(ctrl_obj[job].lastresult) != str(ctrl_obj[job].expected_value):
print "Controller "+ctrl_obj[job].controllername_id+" is not satisfied"
if (ctrl_obj[job].ifnotsatisfied_action) and not(os.path.exists(dirtmp+'KO_'+tmpfile)) and (ctrl_obj[job].repeat_ifnotsatisfied_action=="once"):
logger ("Controller "+ctrl_obj[job].controllername_id+" has started the ifnotsatisfied_action , in "+ctrl_obj[job].repeat_ifnotsatisfied_action,"alert")
logger ("Action: "+ctrl_obj[job].ifnotsatisfied_action,"alert")
os.system(ctrl_obj[job].ifnotsatisfied_action+"> /dev/null")
if (os.path.exists(dirtmp+'OK_'+tmpfile)):
os.remove(dirtmp+'OK_'+tmpfile)
touch(dirtmp+'KO_'+tmpfile)
if (ctrl_obj[job].ifnotsatisfied_action) and (ctrl_obj[job].repeat_ifnotsatisfied_action=="ever"):
logger ("Controller "+ctrl_obj[job].controllername_id+" has started has started the ifnotsatisfied_action, in "+ctrl_obj[job].repeat_ifnotsatisfied_action,"alert")
logger ("Action: "+ctrl_obj[job].ifnotsatisfied_action,"alert")
os.system(ctrl_obj[job].ifnotsatisfied_action+"> /dev/null")
if (os.path.exists(dirtmp+'OK_'+tmpfile)):
os.remove(dirtmp+'OK_'+tmpfile)
touch(dirtmp+'KO_'+tmpfile)
except:
logger ("Unrecovable problem occured: Ouch... something is going wrong please check your scripts and sql query","crit")
print "\n\n!!!!!!!!!!!!Unrecovable problem occured: Ouch... something is going wrong please check your scripts and sql query"
print
print
quit()
def jobexecutesilent(job,startprobe):
try:
executefile =""
par=""
tmpfile=ctrl_obj[job].controllername_id.replace(" ", "_")
i=0
for i in range(startprobe, ctrl_obj[job].totalprobes+startprobe):
if (probe_obj[i].sql) :
dbindex=0
while (probe_obj[i].sqlengine == database_obj[dbindex].dbengine):
dbindex+=1
probe_obj[i].lastresult=executesql(probe_obj[i].sql,dbindex)
#Condition valorizing
ctrl_obj[job].condition=ctrl_obj[job].condition.replace(str(probe_obj[i].probename_id),str(probe_obj[i].lastresult))
else:
#probe_obj[i].lastresult=os.system(probe_obj[i].probefile+" > /dev/null")
probe_obj[i].lastresult=subprocess.check_output(probe_obj[i].probefile, shell=True)
#Condition valorizing
ctrl_obj[job].condition=ctrl_obj[job].condition.replace(str(probe_obj[i].probename_id),str(probe_obj[i].lastresult))
i =+1
ctrl_obj[job].lastresult=executesql(ctrl_obj[job].condition,dbcondition)
#Condition satisfied
if str(ctrl_obj[job].lastresult) == str(ctrl_obj[job].expected_value):
if ctrl_obj[job].rearm_after == 0:
if (ctrl_obj[job].ifsatisfied_action) and not(os.path.exists(dirtmp+'OK_'+tmpfile)) and (ctrl_obj[job].repeat_ifsatisfied_action=="once"):
os.system(ctrl_obj[job].ifsatisfied_action+" > /dev/null")
if (os.path.exists(dirtmp+'KO_'+tmpfile)):
os.remove(dirtmp+'KO_'+tmpfile)
touch(dirtmp+'OK_'+tmpfile)
if (ctrl_obj[job].ifsatisfied_action) and (ctrl_obj[job].repeat_ifsatisfied_action=="ever"):
os.system(ctrl_obj[job].ifsatisfied_action+" > /dev/null")
if (os.path.exists(dirtmp+'OK_'+tmpfile)):
os.remove(dirtmp+'OK_'+tmpfile)
touch(dirtmp+'KO_'+tmpfile)
else:
#REARM
if str(ctrl_obj[job].lastresult) == str(ctrl_obj[job].expected_value) and ctrl_obj[job].rearm_after > 0 and (os.path.exists(dirtmp+'KO_'+tmpfile)):
totalfile=""
totalfile=len(fnmatch.filter(os.listdir(dirtmp), '*.'+tmpfile))
tfile=int(totalfile)+1
touch(dirtmp+str(tfile)+'.'+tmpfile)
toleft=ctrl_obj[job].rearm_after-tfile
if (tfile >=ctrl_obj[job].rearm_after):
os.system(ctrl_obj[job].rearm_action+" > /dev/null")
files = os.listdir(dirtmp)
for f in files:
if not os.path.isdir(f) and tmpfile in f:
os.remove(dirtmp+f)
touch(dirtmp+'OK_'+tmpfile)
#Condition Unsatisfied
if str(ctrl_obj[job].lastresult) != str(ctrl_obj[job].expected_value):
if (ctrl_obj[job].ifnotsatisfied_action) and not(os.path.exists(dirtmp+'KO_'+tmpfile)) and (ctrl_obj[job].repeat_ifnotsatisfied_action=="once"):
os.system(ctrl_obj[job].ifnotsatisfied_action+" > /dev/null")
if (os.path.exists(dirtmp+'OK_'+tmpfile)):
os.remove(dirtmp+'OK_'+tmpfile)
touch(dirtmp+'KO_'+tmpfile)
if (ctrl_obj[job].ifnotsatisfied_action) and (ctrl_obj[job].repeat_ifnotsatisfied_action=="ever"):