#!/usr/bin/python
# -*- coding: utf-8 -*-

# of_serverinfo_mrtg.py
#
# Marcelo H. Terres <mhterres@gmail.com>
# 2014-08-09
#
# Versao 0.1

import sys
import time
import socket

def getdata(sck,cmd):

	sck.send(cmd + '\n')
	time.sleep (0.5)
	data = s.recv(65536)
	return data


host = '127.0.0.1'
port = 4455

# DEBUG
log = open('/tmp/of_serverinfo_mrtg.log', 'a')
sys.stderr = log
#sys.stdout = log

if len(sys.argv) == 1:

	print 'Syntax: of_serverinfo_prtg.py <parameters>.'
	print 'Valid parameters are: online users, total users, server sessions';
	sys.exit(1)

cmds=sys.argv
command=cmds[1]

i=0;

for item in cmds:

	if (i > 1):
		command += " " + item;

	i+=1;

valid_commands=['online users','total users','server sessions']

if not command in valid_commands:

	print 'Invalid command: ' + command
	sys.exit(1)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))

data=getdata(s,command)

print '<html>'
print '<body>'
print command + ' [' + data.replace('\n','') + ']'
print '</body>'
print '</html>'

s.close()

