Advertisement

aws ec2时间_监控aws ec2 instance 的ntp 时间同步状态,并报警

阅读量:

#!/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

TIMENOT.append(ip)

else:

TIMENTPOK.append(ip)

except:

NTPNOT.append(ip)

STRNTPOK=str(TIMENTPOK)

STRTIMENOT=str(TIMENOT)

STRNTPNOT=str(NTPNOT)

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',

]

)

if len(NTPNOT) != 0 or len(TIMENOT) != 0:

sesfunc()

全部评论 (0)

还没有任何评论哟~