-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_process_alive_check.py
86 lines (70 loc) · 2.32 KB
/
pid_process_alive_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import telegram, os, socket, string, sys, psutil, daemon, time
"""
서버 라벨 설정
"""
serverName = 'TEST_SERVER'
title = '[' + serverName + ' 서버]\n'
"""
Telegram bot 설정
"""
sigong_token = 'YOUR_TOKEN'
sigong = 'YOUR_CHAT_ID'
bot = telegram.Bot(token = sigong_token)
"""
감시대상 프로세스 및 인스턴스 명
"""
process = "java"
instance = "-Dserver=instance01"
"""
메세지 송신 함수
"""
def send(chat):
bot.sendMessage(sigong, chat, parse_mode='HTML')
"""
데몬 생성 함수
"""
def daemon():
try:
pid = os.fork()
if pid > 0:
print('PID: %d' % pid)
sys.exit()
except OSError as error:
print('Unable to fork. Error: %d (%s)' % (error.errno, error.strerror))
sys.exit()
doTask()
def doTask():
"new session create"
os.setsid()
os.open("/dev/null", os.O_RDWR)
os.dup(0)
os.dup(0)
p_list=[]
while True:
list_pid_name_cmdline = []
pid_all = []
pid_all.append(pinfo['pid'])
list_pid_name_cmdline.append(pinfo)
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline']) # ex: {'cmdline': ['/usr/sbin/sshd'], 'name': 'sshd', 'pid': 874551} -> print(pinfo['cmdline'], pinfo['name'])
if (pinfo['name'] == process and instance in pinfo['cmdline']):
if pinfo['pid'] not in p_list:
msg = title
msg += 'process UP:' + str(instance) + 'pid is :' + str(pinfo['pid']) + '\n'
p_list.append(pinfo['pid'])
send(msg)
except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
pass
for pid in p_list:
try:
if pid not in pid_all:
p_list.remove(pid)
msg = title
msg += 'Process DOWN / pid:' + str(pid) + '\n'
send(msg)
except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
pass
time.sleep(3)
if __name__ == '__main__':
daemon()