nginx-stat.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python
  2. #
  3. # Options:
  4. #
  5. # -a active
  6. # -a accepted
  7. # -a handled
  8. # -a requests
  9. # -a reading
  10. # -a writing
  11. # -a waiting
  12. #
  13. import sys
  14. import getopt
  15. import urllib2
  16. import re
  17. import ssl
  18. def usage():
  19. print "usage: nginx-stat.py -h 127.0.0.1 -p 80 -a [active|accepted|handled|request|reading|writing|waiting]"
  20. sys.exit(2)
  21. def main():
  22. # Default values
  23. host = "localhost"
  24. port = "80"
  25. getInfo = "None"
  26. proto = "http"
  27. _headers = {}
  28. gcontext = ""
  29. if len(sys.argv) < 2:
  30. usage()
  31. try:
  32. opts, _ = getopt.getopt(sys.argv[1:], "h:p:a:")
  33. except getopt.GetoptError:
  34. usage()
  35. # Assign parameters as variables
  36. for opt, arg in opts:
  37. if opt == "-h":
  38. host = arg
  39. if opt == "-p":
  40. port = arg
  41. if opt == "-a":
  42. getInfo = arg
  43. if port == "443":
  44. proto = "https"
  45. _headers = {'X-Mashape-Key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}
  46. gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  47. url = proto + "://" + host + ":" + port + "/nginx_status/"
  48. request = urllib2.Request(url, headers=_headers)
  49. result = urllib2.urlopen(request, context=gcontext)
  50. buffer = re.findall(r'\d{1,8}', result.read())
  51. ## Format:
  52. ## Active connections: 196
  53. ## server accepts handled requests
  54. ## 272900 272900 328835
  55. ## Reading: 0 Writing: 6 Waiting: 190
  56. if getInfo == "active":
  57. print buffer[0]
  58. elif getInfo == "accepted":
  59. print buffer[1]
  60. elif getInfo == "handled":
  61. print buffer[2]
  62. elif getInfo == "requests":
  63. print buffer[3]
  64. elif getInfo == "reading":
  65. print buffer[4]
  66. elif getInfo == "writing":
  67. print buffer[5]
  68. elif getInfo == "waiting":
  69. print buffer[6]
  70. else:
  71. print "unknown"
  72. sys.exit(1)
  73. if __name__ == "__main__":
  74. main()