Python daemon monitors hive server

Source: Internet
Author: User

Python daemon monitors hive server

1. First add hive thrift server to System Service background run add method address http://jiedushi.blog.51cto.com/673653/608990

2. Use the python daemon to monitor hive server processes. The Code is as follows:

[Html] view plaincopyprint?
  1. #! /Usr/bin/envpython
  2. Importsys, OS, time, atexit, string
  3. FromsignalimportSIGTERM
  4. ClassDaemon:
  5. Def _ init _ (self, pidfile, stdin = '/dev/null', stdout ='/dev/null', stderr = '/dev/null '):
  6. Self. stdin = stdin
  7. Self. stdout = stdout
  8. Self. stderr = stderr
  9. Self. pidfile = pidfile
  10. Def_daemonize (self ):
  11. Try:
  12. Pid = OS. fork ()
  13. Ifpid> 0:
  14. Sys. exit (0)
  15. Optional toserror, e:
  16. Sys. stderr. write ('fork #1 failed: % d (% s) \ n' % (e. errno, e. strerror ))
  17. Sys. exit (1)
  18. OS. setsid ()
  19. OS. chdir ("/")
  20. OS. umask (0)
  21. Try:
  22. Pid = OS. fork ()
  23. Ifpid> 0:
  24. Sys. exit (0)
  25. Optional toserror, e:
  26. Sys. stderr. write ('fork #2 failed: % d (% s) \ n' % (e. errno, e. strerror ))
  27. Sys. exit (1)
  28. Sys. stdout. flush ()
  29. Sys. stderr. flush ()
  30. Si = file (self. stdin, 'R ')
  31. So = file (self. stdout, 'a + ')
  32. Se = file (self. stderr, 'a + ', 0)
  33. OS. dup2 (si. fileno (), sys. stdin. fileno ())
  34. OS. dup2 (so. fileno (), sys. stdout. fileno ())
  35. OS. dup2 (se. fileno (), sys. stderr. fileno ())
  36. Atexit. register (self. delpid)
  37. Pid = str (OS. getpid ())
  38. File (self. pidfile, 'W + '). write (' % s \ n' % pid)
  39. Defdelpid (self ):
  40. OS. remove (self. pidfile)
  41. Defstart (self ):
  42. Try:
  43. Pf = file (self. pidfile, 'R ')
  44. Pid = int (pf. read (). strip ())
  45. Pf. close ()
  46. ExceptIOError:
  47. Pid = None
  48. Ifpid:
  49. Message = 'pidfile % salreadyexist. Daemonalreadyrunning? \ N'
  50. Sys. stderr. write (message % self. pidfile)
  51. Sys. exit (1)
  52. Self. _ daemonize ()
  53. Self. _ run ()
  54. Defstop (self ):
  55. Try:
  56. Pf = file (self. pidfile, 'R ')
  57. Pid = int (pf. read (). strip ())
  58. Pf. close ()
  59. ExceptIOError:
  60. Pid = None
  61. Ifnotpid:
  62. Message = 'pidfile % sdoesnotexist. Daemonnotrunning? \ N'
  63. Sys. stderr. write (message % self. pidfile)
  64. Return
  65. Try:
  66. While1:
  67. OS. kill (pid, SIGTERM)
  68. Time. sleep (0.1)
  69. OS. system ("kill-9 'jps | grepRunJar | awk '{print $1 }''")
  70. Optional toserror, err:
  71. Err = str (err)
  72. Iferr. find ('nosuchprocess')> 0:
  73. Ifos. path. exists (self. pidfile ):
  74. OS. remove (self. pidfile)
  75. Else:
  76. Printstr (err)
  77. Sys. exit (1)
  78. Defrestart (self ):
  79. Self. stop ()
  80. Self. start ()
  81. ClassMyDaemon (Daemon ):
  82. Def_run (self ):
  83. WhileTrue:
  84. Proccess = OS. popen ("jps | grepRunJar | awk '{print $1}' | wc-l"). read (). strip ()
  85. Port = OS. popen ("netstat-tnl | grep10000 | awk '{print $4}' | grep10000 | wc-l"). read (). strip ()
  86. Ifproccess = '0 ':
  87. OS. system ('servicehive-thriftstart ')
  88. Time. sleep (2)
  89. If _ name __= = '_ main __':
  90. Daemon = MyDaemon ('/tmp/watch_process.pid ')
  91. Iflen (sys. argv) = 2:
  92. If 'start' = sys. argv [1]:
  93. Daemon. start ()
  94. Elif 'stop' = sys. argv [1]:
  95. Daemon. stop ()
  96. Elif 'restart' = sys. argv [1]:
  97. Daemon. restart ()
  98. Else:
  99. Print 'unknowncommand'
  100. Sys. exit (2)
  101. Sys. exit (0)
  102. Else:
  103. Print 'usage: % sstart | stop | restart' % sys. argv [0]
  104. Sys. exit (2)

Save the code as hivemonitor. py and grant the executable permission.

Chmod + x hivemonitor. py

Run the script./hivemonitor. py start

The above script passed the test in the centos 5.8 python 2.7 Environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.