Skip to content

Commit

Permalink
Add prometheus counters, logConnection test, monitoring test
Browse files Browse the repository at this point in the history
  • Loading branch information
gfr10598 committed Jan 24, 2017
1 parent f1812e3 commit e6ac982
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
18 changes: 18 additions & 0 deletions exitstats.py
Expand Up @@ -10,6 +10,8 @@
import os
from collections import namedtuple

import prometheus_client as prom

try:
from Web100 import *
except ImportError:
Expand Down Expand Up @@ -43,6 +45,14 @@
"WAD_CwndAdjust"
]

PROMETHEUS_SERVER_PORT = 9090
connection_count = prom.Counter('connection_count',
'Count of connections logged')
plc_connection_count = prom.Counter('plc_connection_count',
'Count of connections to Planet Lab')
main_loop_count = prom.Counter('main_loop_count',
'Count of main loop iterations')

active_vars=None
def setkey(snap):
"""
Expand Down Expand Up @@ -166,8 +176,12 @@ def logConnection(c):
if snap["RemAddress"] == "127.0.0.1":
return
if snap["RemAddress"].startswith("128.112.139"):
plc_connection_count.inc()
return

# Count only the remote connections.
connection_count.inc()

# pick/open a logfile as needed, based on the close poll time
t = time.time()
logf = getLogFile(t, snap["LocalAddress"])
Expand All @@ -190,9 +204,13 @@ def main(argv):
print "Usage: %s [server_name]"%argv[0]
return 0

# Start prometheus server to export metrics.
prom.start_http_server(PROMETHEUS_SERVER_PORT)

agent = Web100Agent()
closed = []
while True:
main_loop_count.inc()
conns = agent.all_connections()
newclosed = []
for c in conns:
Expand Down
43 changes: 41 additions & 2 deletions exitstats_test.py
Expand Up @@ -9,10 +9,49 @@
import unittest
import time
from test.test_support import EnvironmentVarGuard
import prometheus_client as prom
import urllib2

import exitstats

class ExitstatsTest(unittest.TestCase):
class FakeConnection():
'''Substitute for Web100 connection object for testing.'''
cid = 0
values = {}

def readall(self):
'''Returns dictionary of metrics'''
return self.values

def setall(self, v):
self.values = v

def copy(self):
result = FakeConnection()
result.cid = self.cid
result.values = self.values
return result

def __iter__(self):
return values.__iter__()

class TestMonitoring(unittest.TestCase):
def setUp(self):
prom.start_http_server(exitstats.PROMETHEUS_SERVER_PORT)

def testConnectionCount(self):
c1 = FakeConnection()
c1.cid = 1234
c1.setall({"RemAddress": "5.4.3.2", "LocalAddress": "1.2.3.4", "other": "other"})
exitstats.setkey(c1.values)
exitstats.logConnection(c1)

# Read from the httpserver and assert the correct connection count.
print("http://localhost:%s"%(exitstats.PROMETHEUS_SERVER_PORT))
response = urllib2.urlopen(
"http://localhost:%s"%(exitstats.PROMETHEUS_SERVER_PORT)).read()
self.assertTrue("connection_count 1.0" in response)

class TestExitstats(unittest.TestCase):
def remove_file(self, logdir, logname):
''' Utility to remove a file and its directory'''
try:
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
prometheus_client

0 comments on commit e6ac982

Please sign in to comment.