From 169847f0a385f4082a1d99face59bc8dadbfee3f Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Mon, 14 Mar 2016 11:25:09 -0500 Subject: [PATCH] Add command to get current sessions for server in backend. Function returns int if found or None --- bin/haproxyctl | 1 + haproxy/cmds.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/bin/haproxyctl b/bin/haproxyctl index 4afe4bf..d1b9f31 100644 --- a/bin/haproxyctl +++ b/bin/haproxyctl @@ -85,6 +85,7 @@ def main(args): "enable" : cmds.enableServer, "disable" : cmds.disableServer, "get-weight" : cmds.getWeight, + "sessions" : cmds.getServerSessions, "servers" : cmds.listServers, "set-weight" : cmds.setWeight, "frontends" : cmds.showFrontends, diff --git a/haproxy/cmds.py b/haproxy/cmds.py index 80a9879..93609df 100644 --- a/haproxy/cmds.py +++ b/haproxy/cmds.py @@ -207,3 +207,25 @@ def getResultObj(self, res): "bOut: %s" % outCols[cols['bout']]))) return servers + +class getServerSessions(baseStat): + """Get Current Sessions for given server""" + + p_args = ["backend", "server"] + cmdTxt = "show stat\r\n" + helpTxt = "Get current sessions for server in the given backend" + + def getResult(self, res): + if self.args['backend'] is None: + raise Exception("Need to specify backend") + if self.args['server'] is None: + raise Exception("Need to specify server") + cols = self.getCols(res) + + for line in res.split('\n'): + if line.startswith(self.args['backend']): + # Lines for server start with the name of the + # backend. + outCols = line.split(',') + if outCols[cols['svname']] == self.args['server']: + return outCols[cols['scur']]