Commits

Alessandro Crismani  committed b327880

Node.js weserver for monitoring the status of servers: initial commit of the client software

  • Participants

Comments (0)

Files changed (3)

File check-server-status.conf

+author "Alessandro Crismani <alessandro.crismani@gmail.com>"
+description "Send status data to the collecting Web Server"
+
+start on runlevel [2345]
+stop on runlevel [016]
+respawn
+
+chdir /usr/local/bin
+exec python2 check-server-status.py
+

File check-server-status.py

+#!/usr/bin/env python2
+import re
+import platform
+import socket
+import psutil
+import time
+import operator
+import json, urllib2
+
+# Constant values
+MAX_PROC = 10
+MAX_PLOT_DATA = 60
+SERVER = "Input server address here"
+
+class ServerStatus:
+    def __init__(self):
+        # Initialise status
+        self.status = {}
+        # Initialise cpu_usage_list
+        self.cpu_usage = []
+        for cpu in range(psutil.NUM_CPUS):
+            self.cpu_usage.append([])
+        # Initialise ram_usage_list
+        self.ram_usage = [ [] ]
+
+    def main(self):
+        while True:
+            self.check_status()
+            time.sleep(2);
+
+    def check_status(self):
+        self.update_host()
+        self.update_cpu_usage()
+        self.update_ram_usage()
+        self.update_active_users()
+        self.update_active_processes()
+        self.prepare_json()
+        self.send_json(self.prepare_json())
+
+    def update_host(self):
+        self.hostname = socket.gethostname()
+        self.os = platform.system()
+        self.now = time.time() * 1000;
+
+    def update_cpu_usage(self):
+        cpu_now = psutil.cpu_percent(interval=0.0, percpu=True)
+        for idx, cpu in enumerate(cpu_now):
+            self.cpu_usage[idx] = shift(self.cpu_usage[idx], cpu, MAX_PLOT_DATA)
+
+    def update_ram_usage(self):
+        ram_now = psutil.virtual_memory()
+        self.ram_usage[0] = shift(self.ram_usage[0], ram_now.percent, MAX_PLOT_DATA)
+
+    def update_active_users(self):
+        # Empty the dictionary containing active users
+        self.active_users = {}
+        users = psutil.get_users()
+        for user in users:
+            self.active_users[user.name] = True
+
+    def update_active_processes(self):
+        # List all processes running on the system
+        processes = []
+        for ps in psutil.process_iter():
+            if ps.name == "System Idle Process":
+                continue
+            process = {}
+            process["name"] = ps.name
+            try:
+                process["user"] = ps.username.replace(self.hostname + "\\", "")
+            except Exception, e:
+                process["user"] = "not available"
+            process["cpu"] = ps.get_cpu_percent(interval=0.0)
+            process["running-time"] = round(round((time.time() - ps.create_time) / 60) / 60, 2)
+            process["ram"] = round(ps.get_memory_info()[0] / 1000000) # Get the RSS value for the memory usage
+            processes.append(process)
+        processes.sort(key=operator.itemgetter("cpu"), reverse=True)
+        self.active_processes = processes[0:MAX_PROC-1]
+
+    def prepare_json(self):
+        self.status["hostname"] = self.hostname
+        self.status["os"] = self.os
+        self.status["now"] = self.now
+        self.status["cpu"] = self.cpu_usage
+        self.status["logged-users"] = self.active_users
+        self.status["ram"] = self.ram_usage
+        self.status["processes"] = self.active_processes
+        return json.dumps(self.status, separators=(',',':'))
+
+    def send_json(self, data):
+        try:
+            req = urllib2.Request(SERVER + "/" + self.hostname + ".json", data, {'Content-Type': 'application/json'})
+            urllib2.urlopen(req);
+        except Exception, err:
+            print err.reason
+
+def shift(list, value, max_length):
+    if len(list) < max_length:
+        ext = list
+        ext.append(value)
+        return ext
+    else:
+        pop = list[1:len(list)]
+        pop.append(value)
+        return pop
+
+if __name__ == "__main__":
+	status_checker = ServerStatus()
+	status_checker.main()

File send-status.bat

+"C:\Program Files (x86)\Python2\python" "C:\Program Files\ServerStatus\check-server-status.py"