-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpybar_parse
executable file
·94 lines (63 loc) · 2.72 KB
/
pybar_parse
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
#!/usr/bin/env python2.7
#(C)2013 Edwin Eefting edwin@datux.nl
#parses stdin and updates pybar status when a certain regex matches
import subprocess
import re
import sys
import urllib2
import time
###### parse arguments
import argparse
parser = argparse.ArgumentParser(description='pyBar parser v1.0 (C)Edwin Eefting', formatter_class=argparse.RawDescriptionHelpFormatter, epilog="""
Examples:
tail -f /var/log/messages | ./pybar_parse --url http://192.168.13.1:8080 ssh,ok,"sshd.*Accepted.*root" ssh,caution,"sshd.*Failed" arp,ok,"sshd.*Accepted.*root" arp,caution,"arpwatch.*new station"
This watches the logs for failed logins and new stations on your network. You can reset the status back to "ok" by logging in as root. (and checking out the logfiles)
fping -A -b12 -Q2 -l 8.8.8.8 2>&1 | ./pybar_parse --url http://192.168.13.1:8080 inet,ok,/0% inet,caution,/100%
This pings google every second, and generates a caution when there is 100% packet loss. (so you know you're offline within 2 seconds)
""")
parser.add_argument('--url', nargs='?', default='http://localhost:8080', help='pybar url')
parser.add_argument('service', nargs='+', default=[], help='Service definition: name,prio,regex')
#parser.add_argument('--update', action='store_true', help='update existing users')
args = parser.parse_args()
last_prio={}
def update_bar(url, service, prio):
if (service in last_prio) and (last_prio[service]==prio):
return
while 1:
try:
bar_url=url+"/"+service+"/"+prio+"/"
print ("Calling "+bar_url)
urllib2.urlopen(bar_url)
print ("Done")
last_prio[service]=prio
break
except Exception as e:
print ("Opening "+url+" failed:"+str(e))
time.sleep(2)
print ("Retrying...")
services={}
for (service_nr,service) in enumerate(args.service):
(name,prio,regex)=service.split(",")
if name not in services:
services[name]={}
services[name][prio]=regex
for (name,service) in services.items():
update_bar(args.url, name, "ok")
while 1:
try:
line=sys.stdin.readline()
except KeyboardInterrupt:
print("Exitting...")
for (name, service) in services.items():
update_bar(args.url, name, "del")
sys.exit()
if not line:
break
line=str(line)
for (name,service) in services.items():
if 'alert' in service and re.search(service['alert'], line):
update_bar(args.url, name, "alert")
elif 'caution' in service and re.search(service['caution'], line):
update_bar(args.url, name, "caution")
elif 'ok' in service and re.search(service['ok'], line):
update_bar(args.url, name, "ok")