Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. #python module.py 36947 41.221.20.0/24 to test
  4. #python module.py -V -h -v 1 36947 41.221.20.0/24 to test
  5. #
  6. import sys
  7. import time
  8. import procedure
  9.  
  10. state = " "
  11. #ASN = 36947
  12. #prefix = "41.221.20.0/24"
  13.  
  14. def scr(ASN, prefix):
  15. global state
  16. try:
  17. L1, L2 = procedure.procedure(prefix)
  18. #f = open(path, 'r')
  19. lst1 = []
  20. #L1 = f.readline()
  21. for x in L1.split():
  22. lst1.append(int(x))
  23. print (lst1)
  24.  
  25. lst2 = []
  26. #L2 = f.readline()
  27. for x in L2.split():
  28. lst2.append(x)
  29. print (lst2)
  30.  
  31. c = 0
  32. if search(lst1, ASN) & search(lst2, prefix):
  33. c = 1
  34.  
  35. if c == 1:
  36. if (len(lst1) == 1) & (len(lst2) == 1):
  37. output("OK")
  38. elif (len(lst1) == 1) & (len(lst2) != 1):
  39. output("CRITICAL_1")
  40. elif (len(lst1) != 1) & (len(lst2) == 1):
  41. output("CRITICAL_2")
  42. else:
  43. output("UNKNOWN")
  44. else:
  45. output("UNKNOWN")
  46. #f.close()
  47. except Exception:
  48. sys.stdout.write("STATUS: WARNING\n")
  49. exit(1)
  50.  
  51. def search(A, key):
  52. i = 0
  53. while i < len(A) and A[i] != key:
  54. i += 1
  55. return i < len(A)
  56.  
  57. def output(state):
  58. if state == "OK": # в списке more specific будет только один префикс, в списке as-origin будет только один номер
  59. print "STATUS: OK\n"
  60. exit(0)
  61. elif state == "CRITICAL_1": # в списке more specific будет/будут префикс/ы отличные от заданного
  62. print "STATUS: CRITICAL, type = 1\n"
  63. exit(2)
  64. elif state == "CRITICAL_2": # в списке as-origin будет/будут номер/а отличные от заданого
  65. print "STATUS: CRITICAL, type = 2\n"
  66. exit(2)
  67. else:
  68. print "STATUS: UNKNOWN\n"
  69. exit(3)
  70.  
  71. #print "Input data: ASN --", ASN, ", prefix --", prefix
  72.  
  73. #parse_key = sys.argv
  74.  
  75. def parser (key, k):
  76. if key == '-V' or key == '--version':
  77. print "monitoring module 1.0"
  78. elif key == '-h' or key == '--help':
  79. #сделать красиво
  80. print "usage: python module.py [options] <ASN> <prefix> "
  81. print "General Options:"
  82. print " -h, --help Show help."
  83. print " -V, --version Show version."
  84. print " -v [level], --verbose [level] Give more output. "
  85. print " levels:"
  86. print " 0 is for minimal"
  87. print " 1 is for normal"
  88. print " 2 is for detailed"
  89. print " 3 is for diagnostic"
  90. elif k == 1:
  91. if key == '0':
  92. print "Single line, minimal output. Summary"
  93. elif key == '1':
  94. print "Single line, additional information (eg list processes that fail)"
  95. elif key == '2':
  96. print "Multi line, configuration debug output (eg ps command used)"
  97. elif key == '3':
  98. print "Lots of detail for plugin problem diagnosis"
  99. else:
  100. print "try again"
  101. exit(0)
  102. else:
  103. print "try again"
  104. exit(0)
  105.  
  106. st = 1
  107. def keys():
  108. k = 0
  109. global st
  110. if len(sys.argv) > 3:
  111. st = 0
  112. c = 0
  113. while c < (len(sys.argv) - 2):
  114. if k == 1:
  115. parser(sys.argv[c], k)
  116. else:
  117. if sys.argv[c] != 'module.py':
  118. if (sys.argv[c] == '-v') or (sys.argv[c] == '--version'):
  119. k = 1
  120. else:
  121. parser(sys.argv[c], k)
  122. c+=1
  123.  
  124.  
  125. keys()
  126.  
  127. if st == 0:
  128. output("UNKNOWN")
  129. else:
  130. scr(int(sys.argv[len(sys.argv) - 2]), sys.argv[len(sys.argv) - 1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement