forked from NEMSLinux/legacy-nems-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats-livestatus.py
More file actions
executable file
·37 lines (28 loc) · 846 Bytes
/
stats-livestatus.py
File metadata and controls
executable file
·37 lines (28 loc) · 846 Bytes
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
#!/usr/bin/python
import sys
def main():
if len(sys.argv)<3:
socket_path = "/usr/local/nagios/var/rw/live.sock"
query = "hosts"
else:
socket_path = str(sys.argv[1])
query = str(sys.argv[2])
import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(socket_path)
# Write command to socket
s.send("GET " + query + "\n")
# Important: Close sending direction. That way
# the other side knows we are finished.
s.shutdown(socket.SHUT_WR)
# Now read the answer
answer = s.recv(100000000)
result = answer.count('\n')
# Subtract one to accommodate the header output on line 1
result = (result-1)
print result
# Parse the answer into a table (a list of lists)
# table = [ line.split(';') for line in answer.split('\n')[:-1] ]
# print table
if __name__ == '__main__':
main()