#!/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_mrtg.py <parameters>.'
	print 'Valid parameters are: users, 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','users']

run_commands={}
run_commands['online users']=('','online users')
run_commands['total users']=('','total users')
run_commands['server sessions']=('','server sessions')
run_commands['users']=('total users','online users')


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))

if run_commands[command][0] == '':

	data1='0'
else:

	data1=getdata(s,run_commands[command][0])

if run_commands[command][1] == '':

	data2='0'
else:

	data2=getdata(s,run_commands[command][1])

print data1.replace('\n','')
print data2.replace('\n','')
print 0
print 0

s.close()

