-
Notifications
You must be signed in to change notification settings - Fork 2
/
notify.py
127 lines (120 loc) · 5.66 KB
/
notify.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import requests
from requests.auth import HTTPBasicAuth
import ConfigParser
from sys import argv
import os
import logging
class Notify(object):
def __init__(self):
self.element = argv[1]
self.headers = {'Content-Type': 'application/json'}
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='jenkins-monitor.log',
filemode='a')
@staticmethod
def load_config():
email_credentials = {}
config = ConfigParser.ConfigParser()
configFilePath = '/root/jenkins-monitor/configuartion'
os.listdir('.')
config.read(configFilePath)
environment = config.get('parameters', 'environment')
email_username = config.get('parameters', 'email_username')
email_password = config.get("parameters", "email_password")
email_url = config.get("parameters", "email_url")
email_sender = config.get("parameters", "email_sender")
email_updated_password = config.get("parameters", "email_updated_password")
recipients = config.get("parameters", "recipients")
email_credentials["environment"] = environment
email_credentials["email_username"] = email_username
email_credentials["email_password"] = email_password
email_credentials["email_url"] = email_url
email_credentials["email_sender"] = email_sender
email_credentials["email_updated_password"] = email_updated_password
email_credentials["recipients"] = recipients
return email_credentials
@staticmethod
def config_email_service():
notify = Notify()
email_credentials = notify.load_config()
email_username = email_credentials["email_username"]
email_password = email_credentials["email_password"]
email_url = email_credentials["email_url"]
email_sender = email_credentials["email_sender"]
email_updated_password = email_credentials["email_updated_password"]
sub_url = "/management/mailAddresses/"
url = email_url + sub_url + email_sender
register_email = requests.post(url, headers= notify.headers, auth=HTTPBasicAuth(email_username, email_password))
update_url = url + "/password/" + email_updated_password
update_registered_password = requests.put(update_url, headers=notify.headers, auth=HTTPBasicAuth(email_username, email_password))
status_code = [register_email.status_code, update_registered_password.status_code]
print status_code
return status_code
@staticmethod
def send_notification():
notify = Notify()
email_content = {}
email_credentials = notify.load_config()
environment = email_credentials["environment"]
email_username = email_credentials["email_username"]
email_password = email_credentials["email_password"]
email_url = email_credentials["email_url"]
email_sender = email_credentials["email_sender"]
email_updated_password = email_credentials["email_updated_password"]
recipients = email_credentials["recipients"]
email_content["recipients"] = [{"to": recipients}]
email_content["from"] = {"eMail": email_sender, "password": email_updated_password}
alert_type = notify.element
if alert_type == 'jenkins':
content = environment + ' BuildpackManager Jenkins process down'
email_content["subject"] = {"content": content}
email_content["body"] = {
"content": "Alerts! BuildpackManager Jenkins process down!",
"contentType": "text/html"}
elif alert_type == 'cpu':
content = environment + ' BuildpackManager Jenkins vm cpu high'
email_content["subject"] = {"content": content}
email_content["body"] = {
"content": "Alerts! BuildpackManager Jenkins vm cpu high!",
"contentType": "text/html"}
elif alert_type == 'memory':
content = environment + ' BuildpackManager Jenkins vm memory high'
email_content["subject"] = {"content": content}
email_content["body"] = {
"content": "Alerts! BuildpackManager Jenkins vm memory high!",
"contentType": "text/html"}
url = email_url + "/email"
print "url:"
print url
print email_username
print email_password
r = requests.post(url, headers=notify.headers, auth=HTTPBasicAuth(email_username, email_password), json=email_content)
print "r.status_code:"
print r.status_code
return r.status_code
if __name__ == "__main__":
notify = Notify()
if argv[1] == 'register':
status_code = notify.config_email_service()
if status_code[0] == 200:
logging.info("email register successfully")
print("email register successfully")
elif status_code[0] != 200:
logging.info("email register failed")
print("email register failed")
elif status_code[1] == 200:
logging.info("email password update successfully")
print("email password update successfully")
else:
logging.info("email password update failed")
print ("email password update failed")
else:
notification_status_code = notify.send_notification()
if notification_status_code == 200:
logging.info("notification sent successfully")
print("notification sent successfully")
else:
logging.info("notification sent failed")
print("notification sent failed")