stack-monitor: rss feed remembers previous records
[slapos.git] / stack / monitor / monitor.py.in
1 #!/usr/bin/python
2
3 import datetime
4 import json
5 import os
6 import subprocess
7 import sys
8 import time
9 from optparse import OptionParser, make_option
10
11 instance_path = "{{ directory['home'] }}"
12 monitor_dir = "{{ directory['monitor'] }}"
13 pid_dir = "{{ directory['run'] }}"
14 promise_dir = "{{ directory['promise'] }}"
15
16 monitoring_file_json = "{{ monitoring_file_json }}"
17 monitoring_folder_bool = "{{ monitoring_folder_bool }}"
18
19 option_list = [
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")
30 ]
31
32
33 def getListOfScripts(directory):
34 scripts = []
35 if os.path.exists(directory) and os.path.isdir(directory):
36 for file in os.listdir(directory):
37 scripts.append(os.path.join(directory, file))
38 else:
39 exit("There is a problem in your directories" \
40 "of monitoring. Please check them")
41 return scripts
42
43 def runServices(directory):
44 services = getListOfScripts(directory)
45 result = {}
46 for service in services:
47 service_path = os.path.join(pid_dir, service)
48 service_name = os.path.basename(service_path)
49 pid = int(open(service_path).read())
50 try:
51 os.kill(pid, 0)
52 result[service_name] = ''
53 except OSError:
54 result[service_name] = "This service is not running anymore"
55 return result
56
57
58 def runScripts(directory):
59 scripts = getListOfScripts(directory)
60 script_timeout = 3
61 result = {}
62 for script in scripts:
63 command = [os.path.join(promise_dir, script)]
64 script = os.path.basename(command[0])
65 result[script] = ''
66
67 process_handler = subprocess.Popen(command,
68 cwd=instance_path,
69 env=None if sys.platform == 'cygwin' else {},
70 stdout=subprocess.PIPE,
71 stderr=subprocess.PIPE,
72 stdin=subprocess.PIPE)
73 process_handler.stdin.flush()
74 process_handler.stdin.close()
75 process_handler.stdin = None
76
77 time.sleep(script_timeout)
78
79 if process_handler.poll() is None:
80 process_handler.terminate()
81 result[script] = "Time Out"
82 elif process_handler.poll() != 0:
83 stderr = process_handler.communicate()[1]
84 if stderr is not None:
85 result[script] = stderr.strip()
86 return result
87
88
89 def writeFiles(monitors):
90 if len(monitors) == 0:
91 message = "SUCCESS : everything is ok\n"
92 else:
93 message ="FAILURE : something went wrong\n"
94 date = datetime.datetime.now().ctime()
95 monitors['datetime'] = date
96 file_bool = os.path.join(monitoring_folder_bool, str(time.time()))
97 open(file_bool, "w+").write(date + "," + message)
98 open(monitoring_file_json, "w+").write(json.dumps(monitors))
99
100
101 if __name__ == "__main__":
102 parser = OptionParser(option_list=option_list)
103 monitors = {}
104 (options, args) = parser.parse_args()
105
106 if not (options.monitor or options.promise
107 or options.service or options.all):
108 exit("Please provide at list one arg in : -a, -m, -p, -s")
109
110 if options.monitor or options.all:
111 monitors.update(runScripts(monitor_dir))
112 if options.promise or options.all:
113 monitors.update(runScripts(promise_dir))
114 if options.service or options.all:
115 monitors.update(runServices(pid_dir))
116
117 if options.only_stdout:
118 print json.dumps(monitors)
119 else:
120 writeFiles(monitors)
121 if len(monitors) == 0:
122 exit(0)
123 else:
124 exit(1)
125