1 #!{{ python_executable }}
9 from optparse
import OptionParser
, make_option
11 instance_path
= "{{ directory['home'] }}"
12 monitor_dir
= "{{ directory['monitor-custom-scripts'] }}"
13 pid_dir
= "{{ directory['run'] }}"
14 promise_dir
= "{{ directory['promise'] }}"
16 monitoring_file_json
= "{{ monitoring_file_json }}"
17 monitoring_folder_bool
= "{{ directory['monitor-result-bool'] }}"
20 make_option("-a", "--all", action
="store_true", dest
="all",
21 help="test everything : promises, services, customs"),
22 make_option("-n", "--no-write", action
="store_true", dest
="only_stdout",
23 help="just show the json output on stdout"),
24 make_option("-m", "--monitors", action
="store_true", dest
="monitor",
25 help="add the custom monitoring file to the files to monitor"),
26 make_option("-p", "--promises", action
="store_true", dest
="promise",
27 help="add the promises\'file to the files to monitor"),
28 make_option("-s", "--services", action
="store_true", dest
="service",
29 help="add the file containing services\'pid to the files to monitor")
33 def getListOfScripts(directory
):
35 Get the list of script inside of a directory (not recursive)
38 if os
.path
.exists(directory
) and os
.path
.isdir(directory
):
39 for file_name
in os
.listdir(directory
):
40 file = os
.path
.join(directory
, file_name
)
41 if os
.access(file, os
.X_OK
) and not os
.path
.isdir(file):
44 exit("There is a problem in your directories" \
45 "of monitoring. Please check them")
49 def runServices(directory
):
50 services
= getListOfScripts(directory
)
52 for service
in services
:
53 service_path
= os
.path
.join(pid_dir
, service
)
54 service_name
= os
.path
.basename(service_path
)
56 pid
= int(open(service_path
).read())
57 ### because apache (or others) can write sockets
62 result
[service_name
] = ''
64 result
[service_name
] = "This service is not running anymore"
68 def runScripts(directory
):
69 scripts
= getListOfScripts(directory
)
72 for script
in scripts
:
73 command
= [os
.path
.join(promise_dir
, script
)]
74 script
= os
.path
.basename(command
[0])
77 process_handler
= subprocess
.Popen(command
,
79 env
=None if sys
.platform
== 'cygwin' else {},
80 stdout
=subprocess
.PIPE
,
81 stderr
=subprocess
.PIPE
,
82 stdin
=subprocess
.PIPE
)
83 process_handler
.stdin
.flush()
84 process_handler
.stdin
.close()
85 process_handler
.stdin
= None
87 time
.sleep(script_timeout
)
89 if process_handler
.poll() is None:
90 process_handler
.terminate()
91 result
[script
] = "Time Out"
92 elif process_handler
.poll() != 0:
93 stderr
= process_handler
.communicate()[1]
94 if stderr
is not None:
95 result
[script
] = stderr
.strip()
99 def writeFiles(monitors
):
101 for i
in monitors
.values():
105 message
= "FAILURE : something went wrong\n"
107 message
= "SUCCESS : everything is ok\n"
108 date
= datetime
.datetime
.now().ctime()
109 monitors
['datetime'] = date
110 file_bool
= os
.path
.join(monitoring_folder_bool
, str(time
.time()))
111 open(file_bool
, "w+").write(date
+ "," + message
)
112 open(monitoring_file_json
, "w+").write(json
.dumps(monitors
))
115 if __name__
== "__main__":
116 parser
= OptionParser(option_list
=option_list
)
118 (options
, args
) = parser
.parse_args()
120 if not (options
.monitor
or options
.promise
121 or options
.service
or options
.all
):
122 exit("Please provide at list one arg in : -a, -m, -p, -s")
124 if options
.monitor
or options
.all
:
125 monitors
.update(runScripts(monitor_dir
))
126 if options
.promise
or options
.all
:
127 monitors
.update(runScripts(promise_dir
))
128 if options
.service
or options
.all
:
129 monitors
.update(runServices(pid_dir
))
131 if options
.only_stdout
:
132 print json
.dumps(monitors
)
135 if len(monitors
) == 0: