-
Notifications
You must be signed in to change notification settings - Fork 7
/
raid_mon.py
108 lines (86 loc) · 2.76 KB
/
raid_mon.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
#!/usr/bin/env python
# coding:utf-8
import os
import sys
import conf
from lib.log import Log
import signal
import time
import subprocess
from vps_mgr import VPSMgr
import lib.daemon as daemon
import socket
class RaidMonitor(object):
def __init__(self):
self.logger = Log("raid_mon", config=conf)
self.is_running = False
self.last_state = True
self.vps_mgr = VPSMgr()
self.hostname = socket.gethostname()
def start(self):
if self.is_running:
return
self.is_running = True
self.logger.info("started")
def stop(self):
if not self.is_running:
return
self.is_running = False
def send_alarm(self, msg):
rpc = self.vps_mgr.rpc_connect()
try:
rpc.alarm("%s: raid_mon: %s" % (self.hostname, msg))
finally:
rpc.close()
def check(self):
cmd = """MegaCli64 -pdlist -aall | grep -i 'firmware state:' | grep -P -v -i "online|Unconfigured\(good\)" """
try:
out, err = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
msg = out + err
if msg:
self.logger.error(msg)
if self.last_state:
self.last_state = False
self.send_alarm("error, %s" % (out))
self.logger.error("alarm sent")
else:
self.logger.info("ok")
if not self.last_state:
self.send_alarm("ok")
self.last_state = True
except Exception, e:
self.logger.exception(e)
def loop(self):
while self.is_running:
time.sleep(30)
self.check()
stop_signal_flag = False
def _main():
prog = RaidMonitor()
def exit_sig_handler(sig_num, frm):
global stop_signal_flag
if stop_signal_flag:
return
stop_signal_flag = True
prog.stop()
return
prog.start()
signal.signal(signal.SIGTERM, exit_sig_handler)
signal.signal(signal.SIGINT, exit_sig_handler)
prog.loop()
return
def usage():
print "usage:\t%s star/stop/restart\t#manage forked daemon" % (sys.argv[0])
print "\t%s run\t\t# run without daemon, for test purpose" % (sys.argv[0])
os._exit(1)
if __name__ == "__main__":
logger = Log("daemon", config=conf) # to ensure log is permitted to write
pid_file = "raid_mon.pid"
mon_pid_file = "raid_mon_mon.pid"
if len(sys.argv) <= 1:
usage()
else:
action = sys.argv[1]
daemon.cmd_wrapper(action, _main, usage, logger,
conf.log_dir, "/tmp", pid_file, mon_pid_file)
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 :