Skip to content

Instantly share code, notes, and snippets.

@Bharat-B
Created November 29, 2015 18:49
Show Gist options
  • Save Bharat-B/49b341f79d30b6ff06ef to your computer and use it in GitHub Desktop.
Save Bharat-B/49b341f79d30b6ff06ef to your computer and use it in GitHub Desktop.
OpenVZ node information reporter
#!/usr/bin/python
import subprocess
import platform
import urllib2
import urllib
#Add your url to post data here
server_url = "url_to_post_here"
def bash(command):
#print command
a = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
(a, err) = a.communicate()
return a
def fuptime(upseconds):
tseconds = float(upseconds)
# Helper vars:
MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24
# Get the days, hours, etc:
days = int( tseconds / DAY )
hours = int( ( tseconds % DAY ) / HOUR )
minutes = int( ( tseconds % HOUR ) / MINUTE )
seconds = int( tseconds % MINUTE )
string = ""
if days > 0:
string += str(days)+ " " +(days == 1 and "day" or "days" ) + ", "
if len(string) > 0 or hours > 0:
string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", "
if len(string) > 0 or minutes > 0:
string += str(minutes)+ " " +(minutes == 1 and "minute" or "minutes" ) + ", "
string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )
return string;
cpu_model = bash("cat /proc/cpuinfo | grep \"model name\" | tail -n1 | cut -f 2 -d : | tr '\n' ' '")
uptime = bash("cat /proc/uptime | cut -d ' ' -f1 | tr '\n' ' '")
uptime = fuptime(uptime)
load = bash("cat /proc/loadavg | awk '{print $1,$2,$3}' | tr '\n' ' '")
hostname = platform.node()
os = "%s-%s %s"%(platform.dist()[0],platform.dist()[1],platform.machine())
kernel = platform.release()
tram = bash("free | head -n 2 | tail -n 1 | awk '{print $2}'")
tram = int(tram)/1024
uram = bash("free | head -n 3 | tail -n 1 | awk '{print $3}'")
uram = int(uram)/1024
swap = bash("fdisk -l `cat /etc/fstab | grep 'swap' | head -n 1 | awk '{print $1}'` 2>&1 | grep 'Disk ' | grep 'bytes' | awk '{print $5}'")
swap = int(swap)/1024/1024
tdisk = bash("df -h /vz | grep -v \"Filesystem\" |xargs|awk '{print $2}'|sed -e 's/G//'")
tdisk = int(float(tdisk))*1024
udisk = bash("df -h /vz | grep -v \"Filesystem\" |xargs|awk '{print $3}'|sed -e 's/G//'")
udisk = int(float(udisk))*1024
troot = bash("df -h / | grep -v \"Filesystem\" |xargs|awk '{print $2}'|sed -e 's/G//'")
troot = int(float(troot))*1024
uroot = bash("df -h / | grep -v \"Filesystem\" |xargs|awk '{print $3}'|sed -e 's/G//'")
uroot = int(float(uroot))*1024
params = {
'ram' : tram,
'ram_usage' : uram,
'vz' : tdisk,
'vz_usage' : udisk,
'root' : troot,
'root_usage' : uroot,
'uptime' : uptime ,
'load_avg' : load ,
'hostname' : hostname ,
'os' : os,
'kernel' : kernel,
'swap' : swap,
'status' : 'Online'
}
data = urllib.urlencode(params)
req = urllib2.Request(server_url, data)
#Read response uncomment to see response
#response = urllib2.urlopen(req)
#result = response.read()
#print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment