-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_system.py
106 lines (102 loc) · 3.2 KB
/
plugin_system.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
#!/usr/bin/python
import utils
import logger
import config
log = logger.get_logger(__name__)
conf = config.get_config()
# define common commands
commands = {
'cpu_user': {
'command_poll': 'top -bn1',
'command_parse': 'grep "Cpu(s)"|awk \'{print $2}\'',
},
'cpu_system': {
'command_poll': 'top -bn1',
'command_parse': 'grep "Cpu(s)"|awk \'{print $4}\'',
},
'ram_used': {
'command_poll': 'free -m',
'command_parse': 'grep Mem:|awk \'{print $3}\'',
},
'swap_used': {
'command_poll': 'free -m',
'command_parse': 'grep Swap:|awk \'{print $3}\'',
},
'load_1': {
'command_poll': 'uptime',
'command_parse': 'awk \'{gsub(",","",$(NF-2)); print $(NF-2)}\'',
},
'load_5': {
'command_poll': 'uptime',
'command_parse': 'awk \'{gsub(",","",$(NF-1)); print $(NF-1)}\'',
},
'load_15': {
'command_poll': 'uptime',
'command_parse': 'awk \'{gsub(",","",$(NF-0)); print $(NF-0)}\'',
},
'network_services': {
'command_poll': 'netstat -tunap 2>/dev/null',
'command_parse': 'grep tcp|grep LISTEN|wc -l',
},
'network_connections': {
'command_poll': 'netstat -tunap 2>/dev/null',
'command_parse': 'grep tcp|grep -v LISTEN|wc -l',
},
'temperature': {
'command_poll': 'cat /sys/class/thermal/thermal_zone0/temp',
'command_parse': 'awk \'{printf "%.1f",$0/1000}\'',
},
'application_database': {
'command_poll': 'ls -al /var/lib/redis/',
'command_parse': 'grep dump.rdb|awk \'{print $5}\' |grep -o \'[0-9.]\\+\' | awk \'{printf "%.1f",$0/1024/1024}\''
},
'uptime': {
'command_poll': 'cat /proc/uptime',
'command_parse': 'cut -f 1 -d "."'
},
'logwatch': {
'command_poll': 'logwatch --range yesterday --output stdout --format text',
'command_parse': 'cat'
},
'reboot': {
'command_poll': 'reboot',
'command_parse': ''
},
'shutdown': {
'command_poll': 'shutdown -h now',
'command_parse': ''
},
'application_logs': {
'command_poll': 'tail -100 logs/myHouse.log',
'command_parse': 'perl -ne \'/^\\[([^\\]]+)\\] \\[([^\\]]+)\\] (\\w+): (.+)$/;print \"$1|_|$2|_|$3|_|$4\\n\"\''
},
'system_logs': {
'command_poll': 'tail -100 /var/log/messages',
'command_parse': 'perl -ne \'/^(\\S+ \\S+ \\S+) \\S+ (\\S+): (.+)$/;print \"$1|_|$2|_|$3\\n\"\''
}
}
# poll the sensor
def poll(sensor):
if sensor['plugin']['measure'] not in commands:
log.error("Invalid measure "+sensor['plugin']['measure'])
return ""
# run the poll command
command_poll = commands[sensor['plugin']['measure']]["command_poll"]
command = "cd '"+conf["constants"]["base_dir"]+"'; "+command_poll
data = utils.run_command(command,timeout=conf["plugins"]["system"]["timeout"])
return data
# parse the data
def parse(sensor,data):
if sensor['plugin']['measure'] not in commands:
log.error("Invalid measure "+sensor['plugin']['measure'])
return ""
data = str(data).replace("'","''")
command_parse = commands[sensor['plugin']['measure']]["command_parse"]
# no command to run, return the raw data
if command_parse == "": return data
# run command parse
command = "cd '"+conf["constants"]["base_dir"]+"'; echo '"+data+"' |"+command_parse
return utils.run_command(command,timeout=conf["plugins"]["system"]["timeout"])
# return the cache schema
def cache_schema(sensor):
return commands[sensor['plugin']['measure']]['command_poll']