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?
- #! /Usr/bin/envpython
- Importsys, OS, time, atexit, string
- FromsignalimportSIGTERM
- ClassDaemon:
- Def _ init _ (self, pidfile, stdin = '/dev/null', stdout ='/dev/null', stderr = '/dev/null '):
- Self. stdin = stdin
- Self. stdout = stdout
- Self. stderr = stderr
- Self. pidfile = pidfile
- Def_daemonize (self ):
- Try:
- Pid = OS. fork ()
- Ifpid> 0:
- Sys. exit (0)
- Optional toserror, e:
- Sys. stderr. write ('fork #1 failed: % d (% s) \ n' % (e. errno, e. strerror ))
- Sys. exit (1)
- OS. setsid ()
- OS. chdir ("/")
- OS. umask (0)
- Try:
- Pid = OS. fork ()
- Ifpid> 0:
- Sys. exit (0)
- Optional toserror, e:
- Sys. stderr. write ('fork #2 failed: % d (% s) \ n' % (e. errno, e. strerror ))
- Sys. exit (1)
- Sys. stdout. flush ()
- Sys. stderr. flush ()
- Si = file (self. stdin, 'R ')
- So = file (self. stdout, 'a + ')
- Se = file (self. stderr, 'a + ', 0)
- OS. dup2 (si. fileno (), sys. stdin. fileno ())
- OS. dup2 (so. fileno (), sys. stdout. fileno ())
- OS. dup2 (se. fileno (), sys. stderr. fileno ())
- Atexit. register (self. delpid)
- Pid = str (OS. getpid ())
- File (self. pidfile, 'W + '). write (' % s \ n' % pid)
- Defdelpid (self ):
- OS. remove (self. pidfile)
- Defstart (self ):
- Try:
- Pf = file (self. pidfile, 'R ')
- Pid = int (pf. read (). strip ())
- Pf. close ()
- ExceptIOError:
- Pid = None
- Ifpid:
- Message = 'pidfile % salreadyexist. Daemonalreadyrunning? \ N'
- Sys. stderr. write (message % self. pidfile)
- Sys. exit (1)
- Self. _ daemonize ()
- Self. _ run ()
- Defstop (self ):
- Try:
- Pf = file (self. pidfile, 'R ')
- Pid = int (pf. read (). strip ())
- Pf. close ()
- ExceptIOError:
- Pid = None
- Ifnotpid:
- Message = 'pidfile % sdoesnotexist. Daemonnotrunning? \ N'
- Sys. stderr. write (message % self. pidfile)
- Return
- Try:
- While1:
- OS. kill (pid, SIGTERM)
- Time. sleep (0.1)
- OS. system ("kill-9 'jps | grepRunJar | awk '{print $1 }''")
- Optional toserror, err:
- Err = str (err)
- Iferr. find ('nosuchprocess')> 0:
- Ifos. path. exists (self. pidfile ):
- OS. remove (self. pidfile)
- Else:
- Printstr (err)
- Sys. exit (1)
- Defrestart (self ):
- Self. stop ()
- Self. start ()
- ClassMyDaemon (Daemon ):
- Def_run (self ):
- WhileTrue:
- Proccess = OS. popen ("jps | grepRunJar | awk '{print $1}' | wc-l"). read (). strip ()
- Port = OS. popen ("netstat-tnl | grep10000 | awk '{print $4}' | grep10000 | wc-l"). read (). strip ()
- Ifproccess = '0 ':
- OS. system ('servicehive-thriftstart ')
- Time. sleep (2)
- If _ name __= = '_ main __':
- Daemon = MyDaemon ('/tmp/watch_process.pid ')
- Iflen (sys. argv) = 2:
- If 'start' = sys. argv [1]:
- Daemon. start ()
- Elif 'stop' = sys. argv [1]:
- Daemon. stop ()
- Elif 'restart' = sys. argv [1]:
- Daemon. restart ()
- Else:
- Print 'unknowncommand'
- Sys. exit (2)
- Sys. exit (0)
- Else:
- Print 'usage: % sstart | stop | restart' % sys. argv [0]
- 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