#262 [nagios] port haproxy connections script to py3
Merged 3 years ago by mobrien. Opened 3 years ago by mobrien.

@@ -1,4 +1,4 @@

- #!/usr/bin/python2

+ #!/usr/bin/python3

  """ Nagios check for haproxy over-subscription.

  

  fedmsg-gateway is the primary concern as it can eat up a ton of simultaneous
@@ -22,28 +22,28 @@

              return value

  

  

- def query(sockname="/var/run/haproxy-stat"):

+ def query(sockname):

      """ Read stats from the haproxy socket and return a dict """

-     s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

-     s.connect("/var/run/haproxy-stat")

-     s.send('show info\n')

+     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

+     sock.connect(sockname)

+     send_string = 'show info\n'

+     sock.send(send_string.encode())

You could have keep this in one line: sock.send('show info\n'.encode()) but that doesn't really matter

      try:

-         response = s.recv(2048).strip()

+         response = sock.recv(2048).strip().decode()

          lines = response.split('\n')

-         data = dict([map(str.strip, line.split(':')) for line in lines])

-         data = dict([(k, _numeric(v)) for k, v in data.items()])

+         data = dict(map(str.strip, line.split(':')) for line in lines)

+         data = {k:_numeric(v) for (k, v) in data.items()}

          return data

-     except Exception, e:

-         print str(e)

+     except Exception as err:

+         print(str(err))

      finally:

-         s.close()

+         sock.close()

  

      return None

  

  

  def nagios_check(data):

      """ Print warnings and return nagios exit codes. """

- 

      current = data['CurrConns']

      maxconn = data['Maxconn']

      percent = 100 * float(current) / float(maxconn)
@@ -52,25 +52,25 @@

      )

  

      if percent < 50:

-         print "HAPROXY SUBS OK: " + details

+         print("HAPROXY SUBS OK: " + details)

          return 0

  

      if percent < 75:

-         print "HAPROXY SUBS WARN: " + details

+         print("HAPROXY SUBS WARN: " + details)

          return 1

  

      if percent <= 100:

-         print "HAPROXY SUBS CRIT: " + details

+         print("HAPROXY SUBS CRIT: " + details)

          return 2

  

-     print "HAPROXY SUBS UNKNOWN: " + details

+     print("HAPROXY SUBS UNKNOWN: " + details)

      return 3

  

  

  if __name__ == '__main__':

      try:

          data = query(sockname="/var/run/haproxy-stat")

-     except Exception as e:

-         print "HAPROXY SUBS UNKNOWN: " + str(e)

+     except Exception as err:

+         print("HAPROXY SUBS UNKNOWN: " + str(err))

          sys.exit(3)

      sys.exit(nagios_check(data))

no initial comment

FBR to allow the Check proxies for oversubscription nagios check to work on hosts which do not have python2

1 new commit added

  • [nagios] add back in strp accidentally removed
3 years ago

You're loosing the "strip" operation here

I've added this back in, thanks for pointing out

I'm also not sure how dict(list) would work

I was also a bit confused by this at first, from my understanding it works because the line.split creates a 2 item list for each line so the input to dict() is a key value. I admit there was a bit of this is already there so I assume it works to it though.

I was also a bit confused by this at first, from my understanding it works because the line.split creates a 2 item list for each line so the input to dict() is a key value. I admit there was a bit of this is already there so I assume it works to it though.

Seems to work indeed:

>>> lines = ["foo:bar  ", "baz:barf\n"]
>>> dict(map(str.strip, line.split(':')) for line in lines)
{'foo': 'bar', 'baz': 'barf'}

Not the easiest to read piece of code though :(

You could have keep this in one line: sock.send('show info\n'.encode()) but that doesn't really matter

:thumbsup: for me

Note that it was pointed out on the infra list a little while ago that using [] in commit message is not actually a really good idea (can't find the thread atm, will keep looking).

rebased onto 95eb7c7

3 years ago

rebased onto 95eb7c7

3 years ago

Pull-Request has been merged by mobrien

3 years ago