Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. #!/usr/bin/python
  2. graphs = [["h", "hour"], ["d", "day"], ["w", "week"]]
  3.  
  4. viperaddr = "99.198.97.162", 27105
  5. vipereuroaddr = "188.138.113.30", 27015
  6. epicaddr = "epic-empires.co.uk", 27020
  7.  
  8. outdir = "/var/www/html/players/emp/"
  9.  
  10. dbs = "db/emp/viper.rrd", "db/emp/vipereuro.rrd", "db/emp/epic.rrd"
  11. ##############################################################
  12.  
  13. import sys
  14. import time
  15. import rrdtool
  16.  
  17. try:
  18.     from lib.SourceLib import SourceQuery
  19. except Exception, e:
  20.     msg("Import error: %s" % str(e))
  21.     sys.exit()
  22.  
  23. def msg(msg):
  24.     print "(%s) %s" % (time.strftime("%d %b %Y %H:%M:%S"), msg)
  25.  
  26. def get(addr):
  27.     try:
  28.         srv = SourceQuery.SourceQuery(addr[0], addr[1])
  29.         srv.connect()
  30.         return str(srv.info()["numplayers"])
  31.     except Exception, e:
  32.         msg("Error retrieving server data: %s" % str(e))
  33.         return False
  34.  
  35. try:
  36.     #N = time = now
  37.     plys = []
  38.     plys.append(get(viperaddr))
  39.     plys.append(get(vipereuroaddr))
  40.     plys.append(get(epicaddr))
  41.    
  42.     if plys[0] != False: rrdtool.update(dbs[0], "N:" + plys[0])
  43.     if plys[1] != False: rrdtool.update(dbs[1], "N:" + plys[1])
  44.     if plys[2] != False: rrdtool.update(dbs[2], "N:" + plys[2])
  45.     msg("Updated databases")
  46. except Exception, e:
  47.     msg("Error updating DBs: %s" % str(e))
  48.  
  49. for graph in graphs:
  50.     try:
  51.         #--start -1d = start the graph from 1 day ago
  52.         #DEF:<GPRAH variable name>=<rrd file>:<RRD variable name>:RRA
  53.         #%1.1lf would give you a single precision floating point number, like 5.2 or 0.6,
  54.         #and %2.4lf would be a number like 23.1412. Whole number can be rounded with %2.0lf
  55.         rrdtool.graph(outdir + graph[0] + ".png", "--start", "-1" + graph[0],
  56.             "--title=Clients (last " + graph[1] + ")", "--vertical-label=Number of clients",
  57.             "--watermark=ukgamer.dyndns.org",
  58.             "DEF:viperply=" + dbs[0] + ":players:AVERAGE",
  59.             "DEF:vipereuroply=" + dbs[1] + ":players:AVERAGE",
  60.             "DEF:epicply=" + dbs[2] + ":players:AVERAGE",
  61.             "LINE2:viperply#FF0000:VIPER",
  62.             "LINE2:vipereuroply#00FF00:VIPER Euro",
  63.             "LINE2:epicply#0000FF:EPIC",
  64.             "COMMENT:\\n",
  65.             "GPRINT:viperply:MIN:VIPER Min\: %2.0lf",
  66.             "GPRINT:viperply:LAST:Cur\: %2.0lf",
  67.             "GPRINT:viperply:MAX:Max\: %2.0lf",
  68.             "GPRINT:viperply:AVERAGE:Avg\: %2.1lf\\r",
  69.             #########################################
  70.             "GPRINT:vipereuroply:MIN:VIPER Euro Min\: %2.0lf",
  71.             "GPRINT:vipereuroply:LAST:Cur\: %2.0lf",
  72.             "GPRINT:vipereuroply:MAX:Max\: %2.0lf",
  73.             "GPRINT:vipereuroply:AVERAGE:Avg\: %2.1lf\\r",
  74.             #########################################
  75.             "GPRINT:epicply:MIN:EPIC Min\: %2.0lf",
  76.             "GPRINT:epicply:LAST:Cur\: %2.0lf",
  77.             "GPRINT:epicply:MAX:Max\: %2.0lf",
  78.             "GPRINT:epicply:AVERAGE:Avg\: %2.1lf\\r",
  79.             "COMMENT:Last update\: " + time.strftime("%d %b %Y %H\:%M\:%S %Z"))
  80.             #strftime because i have no idea how to do it the rrdtool way
  81.         msg("Updated graph (" + graph[0] + ")")
  82.     except Exception, e:
  83.         msg("Error updating graph: %s" % str(e))
Add Comment
Please, Sign In to add comment