check_conntrack_size 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python3
  2. #
  3. # Nagios plugin to check netfilter conntrack size
  4. #
  5. # Maximilian Wilhelm <max@rfc2324.org>
  6. # -- Fri 11 Mar 2016 08:56:08 PM CET
  7. #
  8. import argparse
  9. import os.path
  10. import sys
  11. code = 0
  12. msg = ""
  13. parser = argparse.ArgumentParser (description = 'check netfilter conntrack table size')
  14. parser.add_argument ('--warn', '-w', help = "Warning conntrack table usage (percent)", default = "70", type = int)
  15. parser.add_argument ('--crit', '-c', help = "Critical conntrack table usage (percent)", default = "85", type = int)
  16. parser.add_argument ('--no-conntrack', help = "Return code when no conntrack is loaded.", default = "ok", choices = [ "ok", "warn", "crit", "unkn" ])
  17. args = parser.parse_args ()
  18. ret_map = {
  19. 'ok' : 0,
  20. 'warn' : 1,
  21. 'crit' : 2,
  22. 'unkn' : 3,
  23. }
  24. def read_int (path):
  25. try:
  26. with open (path, 'r') as fh:
  27. return float (fh.read ())
  28. except ValueError as v:
  29. return -1
  30. except IOError as i:
  31. print ("conntrack seems not to be loaded.")
  32. sys.exit (ret_map[args.no_conntrack])
  33. num_entries = read_int ("/proc/sys/net/netfilter/nf_conntrack_count")
  34. max_entries = read_int ("/proc/sys/net/netfilter/nf_conntrack_max")
  35. usage = num_entries / max_entries * 100
  36. if usage >= args.crit:
  37. code = 2
  38. msg = "Conntrack pool usage over %d%%: %d (%d / %d)" % (args.crit, usage, num_entries, max_entries)
  39. elif usage >= args.warn:
  40. code = 1
  41. msg = "Conntrack pool usage over %d%%: %d (%d/ %d)" % (args.warn, usage, num_entries, max_entries)
  42. elif usage < args.warn:
  43. code = 0
  44. msg = "Conntrack pool usage at %d%% (%d / %d)" % (usage, num_entries, max_entries)
  45. else:
  46. code = 3
  47. msg = "WTF? Please examinte the situation manually and kinly do the needful!"
  48. print (msg)
  49. sys.exit (code)