File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ import  os 
2+ import  smtplib 
3+ import  requests 
4+ # import logging 
5+ from  linode_api4  import  LinodeClient , Instance 
6+ 
7+ EMAIL_ADDRESS  =  os .environ .get ('EMAIL_USER' )
8+ EMAIL_PASSWORD  =  os .environ .get ('EMAIL_PASS' )
9+ LINODE_TOKEN  =  os .environ .get ('LINODE_TOKEN' )
10+ 
11+ # logging.basicConfig(filename='PATH_TO_DESIRED_LOG_FILE', 
12+ #                     level=logging.INFO, 
13+ #                     format='%(asctime)s:%(levelname)s:%(message)s') 
14+ 
15+ 
16+ def  notify_user ():
17+     with  smtplib .SMTP ('smtp.gmail.com' , 587 ) as  smtp :
18+         smtp .ehlo ()
19+         smtp .starttls ()
20+         smtp .ehlo ()
21+ 
22+         smtp .login (EMAIL_ADDRESS , EMAIL_PASSWORD )
23+ 
24+         subject  =  'YOUR SITE IS DOWN!' 
25+         body  =  'Make sure the server restarted and it is back up' 
26+         msg  =  f'Subject: { subject } \n \n { body }  ' 
27+ 
28+         # logging.info('Sending Email...') 
29+         smtp .sendmail (EMAIL_ADDRESS , 'INSERT_RECEIVER_ADDRESS' , msg )
30+ 
31+ 
32+ def  reboot_server ():
33+     client  =  LinodeClient (LINODE_TOKEN )
34+     my_server  =  client .load (Instance , 376715 )
35+     my_server .reboot ()
36+     # logging.info('Attempting to reboot server...') 
37+ 
38+ 
39+ try :
40+     r  =  requests .get ('https://example.com' , timeout = 5 )
41+ 
42+     if  r .status_code  !=  200 :
43+         # logging.info('Website is DOWN!') 
44+         notify_user ()
45+         reboot_server ()
46+     else :
47+         # logging.info('Website is UP') 
48+ except  Exception  as  e :
49+     # logging.info('Website is DOWN!') 
50+     notify_user ()
51+     reboot_server ()
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments