Skip to content

Instantly share code, notes, and snippets.

@K-Wu
Created August 3, 2017 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K-Wu/0e14a6bc4e6781baa46ea7e979ce975e to your computer and use it in GitHub Desktop.
Save K-Wu/0e14a6bc4e6781baa46ea7e979ce975e to your computer and use it in GitHub Desktop.
report server running status through sendgrid
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
'''
author: K-Wu
filename: monitor
Python version: 2.7
IDE version: PyCharm
created time: 8/3/2017
not compatible with python3,for the SyntaxError: import * only allowed at module level
'''
import time
import os
import sys
if sys.version_info[0]==3:
import requests
filename=str(time.time())
def send_message_through_grid(text):
#sendgrid curlapi
command="""
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header "Authorization: Bearer <YOUR_API_KEY>" \
--header 'Content-Type: application/json' \
--data-binary --data '{"personalizations": [{"to": [{"email": "<YOUR_MAIL_ADDRESS>"}]}],"from": {"email": "admin@tsinghua.fit"},"subject": "Server Hourly Report","content": [{"type": "text/plain", "value": \
"""
command+='"'
command+=text
command+='"'
command+="}]}'"
print(command)
os.system(command)
return
def send_message_through_grid_api(text):
#sendgrid python api
import sendgrid
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(apikey="<YOUR_API_KEY>")
from_email = Email("admin@tsinghua.fit")
to_email = Email("<YOUR_EMAIL_ADDRESS>")
subject = "Server Running Status Report"
content = Content("text/plain", text)
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
def send_simple_message(text):
#mailgun request api
return requests.post(
"https://api.mailgun.net/v3/<YOUR_MAILGUN_DOMAIN>/messages",
auth=("api", "<YOUR_API_KEY>"),
data={"from": "Wu Kun-automated <wuk15@<YOUR_MAILGUN_DOMAIN>>",
"to": ["<YOUR_EMAIL_ADDRESS>"],
"subject": "Hourly report from FIT@16",
"text": text})
#a=send_message_through_grid_api("This is a very interesting topic")
text=""
with open(filename,'w') as fd:
text+="date \n\n"
text+=os.popen("date").read()
text+=" \n"
text+=" ========now running top|head======== \n"
text+=os.popen("top -b| head -n 5").read().replace("\n"," \n")
text+=" =========now running ifconfig=========== \n"
text+=os.popen("ifconfig").read()
text+=" ========process owned by wk15======== \n"
text+=os.popen("ps -ef | grep wk15").read().replace("\n"," \n")
text+=" ===================================== \n"
text+="DO NOT REPLY TO THIS MAIL. If you have questions, please contact wuk15 <at> mails.tsinghua.edu.cn"
fd.write(text)
try:
send_message_through_grid_api(text)
except Exception:
pass
os.system("sendmail <"+filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment