티스토리 뷰

 

안녕하세요. 

제가 구동하고 있는 서버가 (혹은 network) 가끔 동작 안 하는 경우가 있는데, 이 때 핸드폰으로 알람을 주는 기능을 구현해봤습니다. 

 



1. Server status check

1) 우선 ping 을 통해서 정상적으로 network 연결이 되는지 check합니다. 

2) -c (count) 2개를 던져서 정상 여부 확인  

 

[function]

check_server_status(hostName)

 

2. Slack API를 통해 notification 전달

1) Slack API 를 통해서 notification 을 등록하여 event post합니다. 

2) token 은 아래 slack api에서 얻어옵니다.

api.slack.com/

 

3)channel 생성하여 해당 channel 에 post합니다. 

slack.chat.post_message('#channel', message)

 

[function]

slack_post(message)

 

[전체 코드]

# -*- coding: utf-8 -*- 
import os
from slacker import Slacker
import time, datetime
#import schedule
 
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
slack = Slacker(token)
 
 
def check_server_status(hostName):
 
    response = os.system("ping -c 2 " + hostName)
    
    if response == 0:
        Networkstatus = "Active" #Network Active
        
    else:
        Networkstatus = "Deactive!!!" #Network Error
 
    return Networkstatus
 
def slack_post(message) :
     
    now = time.localtime()
    #print("%04d/%02d/%02d %02d:%02d:%02d \n" % (now.tm_year,
    #                                                  now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec))
    slack.chat.post_message('#j1server_active_noti1'"J1 & RP4 Server is "+ message+ "\n %04d/%02d/%02d %02d:%02d:%02d \n" % (now.tm_year,
                                                      now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec) )
 
 
 
if __name__ == "__main__":
 
    ret=check_server_status('aaa.co.kr')
    slack_post(ret)
 
    #if ret == "Deactive!!!" :
    #    slack_post(ret)
    
    print(ret)
 
 
cs
 

정상동작하는 화면입니다. 

 

그럼 수고하세요

 

[참고문헌] 
appia.tistory.com/299

 

 

 

댓글
«   2024/09   »
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
글 보관함