#!/usr/bin/python

import base64
import httplib, urllib
import sys
import datetime

DEBUG = 0

def getbasic(user, password):
    # basic authentication (according to HTTP)
    return base64.encodestring(user + ":" + password)    

#
#
#
#
if DEBUG:
	f=open('/tmp/dss-check.log', 'a')
	tolog=str(datetime.datetime.now())+" "+str(sys.argv)+"\n"
	f.write(tolog)
	f.close()


if len(sys.argv) != 5:
	sys.exit(-1)

hostname = sys.argv[1]
port = sys.argv[2]
login = sys.argv[3]
passwd = sys.argv[4]

params=""
page = "/modules/admin/server/*?command=get+filter1=qtssRTPSvrCurConn+filter2=qtssMP3SvrCurConn"

headers = {"User-Agent": "Cacti HTML", "Connection": "close", "Authorization": "Basic "+getbasic(login, passwd), "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection(hostname,port)

conn.request("GET", page,params, headers)
response = conn.getresponse()
if DEBUG:
	print response.status, response.reason

data = response.read()
conn.close()
lines = data.split("\n")
output_line = ""
for line in lines:
	if line.find("=") > 0:
		key,value = line.split("=")
		if key == "qtssRTPSvrCurConn":
			output_line += " dssCurrentConnection:"+value.strip("\"")
		if key == "qtssMP3SvrCurConn":
			output_line += " dssMP3CurrentConnection:"+value.strip("\"")
print output_line.lstrip()
