#276 nagios server plugins: port to py3
Merged 3 years ago by kevin. Opened 3 years ago by mobrien.

@@ -1,26 +1,28 @@

- #!/usr/bin/python2

+ #!/usr/bin/python

  """ NRPE check for datanommer/fedmsg health.

  Given a category like 'bodhi', 'buildsys', or 'git', return an error if

  datanommer hasn't seen a message of that type in such and such time.

  You can alternatively provide a 'topic' which might look like

  org.fedoraproject.prod.bodhi.update.comment.

- 

+  

  Requires:  python-dateutil

- 

+  

  Usage:

- 

+  

      $ check_datanommer_timesince CATEGORY WARNING_THRESH CRITICAL_THRESH

- 

+  

  :Author: Ralph Bean <rbean@redhat.com>

- 

+  

  """

- 

+ from __future__ import print_function

+  

+ from builtins import str

  import dateutil.relativedelta

  import subprocess

  import sys

  import json

- 

- 

+  

+  

  def query_timesince(identifier):

      # If it has a '.', then assume it is a topic.

      if '.' in identifier:
@@ -34,39 +36,39 @@

      prefix, stdout = stdout.split("INFO] ", 1)

      data = json.loads(stdout)

      return float(data[0])

- 

- 

+  

+  

  def main():

      identifier, warning_threshold, critical_threshold = sys.argv[-3:]

      timesince = query_timesince(identifier)

      warning_threshold = int(warning_threshold)

      critical_threshold = int(critical_threshold)

- 

+  

      time_strings = []

      rd = dateutil.relativedelta.relativedelta(seconds=timesince)

      for denomination in ['years', 'months', 'days', 'hours', 'minutes', 'seconds']:

          value = getattr(rd, denomination, 0)

          if value:

              time_strings.append("%d %s" % (value, denomination))

- 

+  

      string = ", ".join(time_strings)

      reason = "datanommer has not seen a %r message in %s" % (identifier, string)

- 

+  

      if timesince > critical_threshold:

-         print "CRIT: ", reason

+         print("CRIT: ", reason)

          sys.exit(2)

- 

+  

      if timesince > warning_threshold:

-         print "WARN: ", reason

+         print("WARN: ", reason)

          sys.exit(1)

- 

-     print "OK: ", reason

+  

+     print("OK: ", reason)

      sys.exit(0)

- 

- 

+  

+  

  if __name__ == '__main__':

      try:

          main()

      except Exception as e:

-         print "UNKNOWN: ", str(e)

+         print("UNKNOWN: ", str(e))

          sys.exit(3)

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

- #!/usr/bin/python2

+ #!/usr/bin/python

+ from __future__ import print_function

+ from builtins import str

  import sys

  

  try:
@@ -9,15 +11,15 @@

  

      items = queue.length

      if items > 500:

-         print "CRITICAL:  %i tasks in fcomm queue" % items

+         print("CRITICAL:  %i tasks in fcomm queue" % items)

          sys.exit(2)

      elif items > 250:

-         print "WARNING:  %i tasks in fcomm queue" % items

+         print("WARNING:  %i tasks in fcomm queue" % items)

          sys.exit(1)

      else:

-         print "OK:  %i tasks in fcomm queue" % items

+         print("OK:  %i tasks in fcomm queue" % items)

          sys.exit(0)

  

  except Exception as e:

-     print "UNKNOWN:", str(e)

+     print("UNKNOWN:", str(e))

      sys.exit(3)

@@ -1,17 +1,19 @@

- #!/usr/bin/python2

- 

+ #!/usr/bin/python

+  

+ from __future__ import print_function

  import fcntl

  import sys

- 

+  

  try:

      f = open('/mnt/koji/.nagios_test', 'r')

      f.close()

      f = open('/mnt/koji/.nagios_test', 'w')

- except IOError:

-     print "Could not create file"

+ except IOError as e:

+     print(e)

+     print("Could not create file")

      sys.exit(2)

      

  fcntl.flock(f, fcntl.LOCK_EX)

  f.close()

- print "File Locked Successfully"

+ print("File Locked Successfully")

  sys.exit(0)

@@ -1,20 +1,23 @@

- #!/usr/bin/python2

+ #!/usr/bin/python

  """ check_supybot_plugin -- ensure that a plugin is loaded by supybot.

- 

+  

  Run like:

- 

+  

      check_supybot_plugin --target fedmsg

      check_supybot_plugin --target koji --debug

- 

+  

  """

- 

+ from __future__ import print_function

+  

+ from builtins import str

+ from builtins import map

  import argparse

  import sys

  import socket

  import string

  import uuid

- 

- 

+  

+  

  def process_args():

      parser = argparse.ArgumentParser()

      parser.add_argument(
@@ -38,71 +41,71 @@

          help='Host to connect to.', dest='port',

      )

      return parser.parse_args()

- 

+  

  args = process_args()

- 

+  

  # Use a random nick so people can't mess with us

  if not args.nick:

      args.nick = 'nrpe-' + str(uuid.uuid4()).split('-')[0]

- 

+  

  name = "NRPE Bot"

  readbuffer = ""

- 

+  

  if not args.target:

-     print "UNKNOWN:  No 'target' specified."

+     print("UNKNOWN:  No 'target' specified.")

      sys.exit(3)

- 

+  

  args.target = args.target.lower()

- 

+  

  if args.debug:

-     print "connecting to %s/%i" % (args.host, args.port)

- 

+     print("connecting to %s/%i" % (args.host, args.port))

+  

  try:

      s = socket.socket()

      s.connect((args.host, args.port))

- 

+  

      if args.debug:

-         print "as %s/%s (%s)" % (args.nick, args.nick, name)

- 

-     s.send("nick %s\r\n" % args.nick)

-     s.send("USER %s %s bla :%s\r\n" % (args.nick, args.host, name))

- 

+         print("as %s/%s (%s)" % (args.nick, args.nick, name))

+  

+     s.send(("nick %s\r\n" % args.nick).encode())

+     s.send(("USER %s %s bla :%s\r\n" % (args.nick, args.host, name)).encode())

+  

      while 1:

-         readbuffer = readbuffer+s.recv(1024)

-         temp = string.split(readbuffer, "\n")

+         readbuffer = readbuffer+s.recv(1024).decode()

+         temp = str.split(readbuffer, "\n")

          readbuffer = temp.pop()

- 

+  

          for line in temp:

-             line = string.rstrip(line)

- 

+             line = str.rstrip(line)

+  

              if args.debug:

-                 print " * ", line

- 

-             line = string.split(line)

- 

+                 print(" * ", line)

+  

+             line = str.split(line)

+  

              if line[1] == 'MODE':

-                 msg = "privmsg zodbot :list\r\n"

+                 msg = "privmsg zodbot :list\r\n".encode()

                  if args.debug:

-                     print "sending:"

-                     print " ->", msg

+                     print("sending:")

+                     print(" ->", msg)

                  s.send(msg)

- 

+  

              if line[0] == ':zodbot!supybot@fedora/bot/zodbot':

                  if args.debug:

-                     print "Got our response.."

- 

-                 plugins = map(str.lower, ' '.join(line[3:][1:]).split(', '))

- 

+                     print("Got our response..")

+  

+                 plugins = list(map(str.lower, ' '.join(line[3:][1:]).split(', ')))

+  

                  if args.target in plugins:

-                     print "OK"

-                     s.send("QUIT")

+                     print("OK")

+                     s.send("QUIT".encode())

                      sys.exit(0)

                  else:

-                     print "CRITICAL: %r not loaded by supybot" % args.target

-                     s.send("QUIT")

+                     print("CRITICAL: %r not loaded by supybot" % args.target)

+                     s.send("QUIT".encode())

                      sys.exit(2)

  except Exception as e:

-     print "UNKNOWN: ", str(e)

+     print("UNKNOWN: ", str(e))

      if args.debug:

          raise

      sys.exit(3)