#!/usr/bin/env python
import ntplib
import os
import time
import json
import boto3
ENV=os.getenv('env')
REFRESH=os.system("/xxx/ansible/ec2.py --refresh-cache 1&>2 >>/dev/null")
jsonFile = open("/xx/xxx/xxx//ansible-ec2.cache",'r')
jsonString = jsonFile.read()
jsonData = json.loads(jsonString)
EC2LIST=jsonData['tag_Environment_'+ENV]
TIMENTPOK=[]
NTPNOT=[]
TIMENOT=[]
jsonFile.close()
def ntpserver():
c = ntplib.NTPClient()
response = c.request("time-a-g.nist.gov")
global NTPTIME
NTPTIME=response.tx_time
return NTPTIME
for ip in EC2LIST:
c = ntplib.NTPClient()
try:
response = c.request(ip)
if response:
INSTANCESYSTIME=response.tx_time
ntpserver()
if INSTANCESYSTIME-NTPTIME>5 or INSTANCESYSTIME-NTPTIME<-5:
TIMENOT.append(ip)
else:
TIMENTPOK.append(ip)
except:
NTPNOT.append(ip)
STRNTPOK=str(TIMENTPOK)
STRTIMENOT=str(TIMENOT)
STRNTPNOT=str(NTPNOT)python
client = boto3.client('ses')
def sesfunc():
sesresponse = client.send_email(
Source='xxx@xxx.com',
Destination={
'ToAddresses': [
'xxx@xxx.com',
]
},
Message={
'Subject': {
'Data': ENV+' NTP status alert'
},
'Body': {
'Text': {
'Data':'NTP problem instance list '+STRNTPNOT+' '+STRTIMENOT
}
}
},
ReplyToAddresses=[
'xxx@xxx.com',
]
)json
if len(NTPNOT) != 0 or len(TIMENOT) != 0:
sesfunc()app