2012-04-07 23:01:28 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Copyright 2012 Dan Ballard and Robert Hausch
|
|
|
|
#
|
|
|
|
## License:
|
|
|
|
#
|
|
|
|
# This file is part of System-Monitoring-Daemon.
|
|
|
|
#
|
|
|
|
# System-Monitoring-Daemon is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2012-04-08 03:36:32 +00:00
|
|
|
# the Free Software Foundation, version 3 of the License.
|
2012-04-07 23:01:28 +00:00
|
|
|
#
|
|
|
|
# System-Monitoring-Daemon is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with System-Monitoring-Daemon. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
2012-04-07 23:43:54 +00:00
|
|
|
import threading
|
2012-04-07 23:01:28 +00:00
|
|
|
from socket import *
|
2012-04-08 00:37:15 +00:00
|
|
|
import time
|
2012-04-07 23:01:28 +00:00
|
|
|
|
2012-04-08 03:36:32 +00:00
|
|
|
#from sensors.cpu import *
|
|
|
|
#from sensors.memory import *
|
|
|
|
import sensors
|
2012-04-07 23:47:30 +00:00
|
|
|
|
2012-04-07 23:01:28 +00:00
|
|
|
HOST = ''
|
|
|
|
PORT = 6000
|
|
|
|
ADDR = (HOST, PORT)
|
|
|
|
BUFSIZE = 4096
|
|
|
|
MAX_QUEUE = 5
|
|
|
|
|
|
|
|
|
2012-04-08 00:37:15 +00:00
|
|
|
class Stats():
|
2012-04-07 23:01:28 +00:00
|
|
|
def __init__(self):
|
2012-04-07 23:43:54 +00:00
|
|
|
self.read_lock = threading.Lock()
|
|
|
|
self.write_lock = threading.Lock()
|
|
|
|
|
2012-04-08 03:36:32 +00:00
|
|
|
self.sensors = sensors.sensors #[]
|
|
|
|
#self.sensors.append(cpu_monitor())
|
|
|
|
#self.sensors.append(mem_monitor())
|
2012-04-08 00:37:15 +00:00
|
|
|
|
|
|
|
self.stop = threading.Event()
|
|
|
|
t = threading.Thread(target=self.update_loop, args=())
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
def update_loop(self):
|
|
|
|
while not self.stop.isSet():
|
|
|
|
self.acquire_write()
|
|
|
|
for s in self.sensors:
|
|
|
|
s.update()
|
|
|
|
self.release_write()
|
2012-04-08 04:11:52 +00:00
|
|
|
time.sleep(0.1)
|
2012-04-08 00:37:15 +00:00
|
|
|
|
2012-04-07 23:47:30 +00:00
|
|
|
def getStats(self):
|
2012-04-08 00:46:42 +00:00
|
|
|
data = []
|
2012-04-08 00:53:47 +00:00
|
|
|
self.acquire_read()
|
2012-04-07 23:47:30 +00:00
|
|
|
for s in self.sensors:
|
2012-04-08 00:46:42 +00:00
|
|
|
data.append(s.getFormatedData())
|
|
|
|
message = '{"Stats":['+','.join('%s'%x for x in data)+']}'
|
|
|
|
|
2012-04-08 00:53:47 +00:00
|
|
|
self.release_read()
|
2012-04-07 23:47:30 +00:00
|
|
|
return message
|
|
|
|
|
2012-04-07 23:43:54 +00:00
|
|
|
def acquire_read(self):
|
|
|
|
self.read_lock.acquire()
|
|
|
|
self.write_lock.acquire()
|
|
|
|
|
|
|
|
def release_read(self):
|
|
|
|
self.write_lock.release()
|
|
|
|
self.read_lock.release()
|
|
|
|
|
|
|
|
def acquire_write(self):
|
|
|
|
self.write_lock.acquire()
|
|
|
|
|
|
|
|
def release_write(self):
|
|
|
|
self.write_lock.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-07 23:01:28 +00:00
|
|
|
class ClientHandler:
|
|
|
|
def __init__(self, stats):
|
|
|
|
self.stats = stats
|
|
|
|
|
|
|
|
def handle(self, sock):
|
2012-04-07 23:47:30 +00:00
|
|
|
conn_sock.send( self.stats.getStats() )
|
2012-04-07 23:01:28 +00:00
|
|
|
conn_sock.close()
|
|
|
|
|
2012-04-08 00:37:15 +00:00
|
|
|
|
2012-04-07 23:01:28 +00:00
|
|
|
stats = Stats()
|
2012-04-08 00:37:15 +00:00
|
|
|
|
2012-04-07 23:01:28 +00:00
|
|
|
|
|
|
|
serv_sock = socket(AF_INET, SOCK_STREAM)
|
|
|
|
# Behave better after crash
|
|
|
|
serv_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
|
|
|
|
|
|
|
|
|
|
|
serv_sock.bind((ADDR)) #tuple with one param
|
|
|
|
serv_sock.listen(MAX_QUEUE)
|
|
|
|
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
(conn_sock,addr) = serv_sock.accept()
|
|
|
|
except KeyboardInterrupt:
|
2012-04-08 00:37:15 +00:00
|
|
|
print "\nExiting..."
|
2012-04-07 23:01:28 +00:00
|
|
|
break;
|
|
|
|
except:
|
|
|
|
conn_sock = 0
|
|
|
|
if conn_sock:
|
|
|
|
#fork
|
|
|
|
#conn_sock.send('{ cpu_load: %d}' % (stats.cpu_load))
|
|
|
|
client = ClientHandler(stats)
|
|
|
|
client.handle(conn_sock)
|
|
|
|
|
2012-04-08 00:37:15 +00:00
|
|
|
stats.stop.set()
|
2012-04-07 23:01:28 +00:00
|
|
|
serv_sock.close()
|
|
|
|
|