-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
191 lines (151 loc) · 5.6 KB
/
server.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import sqlite3, smtplib, os, subprocess
from flask import Flask, request, session, g, render_template, flash, \
redirect, url_for, abort # import missing abort
from contextlib import closing
from email import MIMEMultipart # not need from email.abc import abc; just from email import abc
from email import MIMEText
# for hashing password, it can be use SHA256 or scrypt; Depend on your favor
# App Settings - Should be put in a separate file, but let be it for this app
DATABASE = "db/servers.db"
DEBUG = True
# this key is generated by os.urandom(24)
SECRET_KEY = "\r#t\xb4\xba/\x96\x9fJ\x05\xa2\x0f(\x9e=7\xeeDg\x85\x1c@\x89\xc1"
# Configuration for sending email
sender = "[email protected]"
senderPass = "aaa"
receiver = "[email protected]"
app = Flask(__name__)
app.config.from_object(__name__) # Initialize app's settings
# Helper function to connect to SQLite3
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
# Helper function to init SQLite3 db when app starts
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
# Initialize DB connection for each request and tear them down afterwards
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
# Ping a specific host and report its status
def isAlive(server):
command = ['ping', '-n', '1', '-w', '1000', server]
with open(os.devnull, 'w') as DEVNULL:
res = subprocess.call(command, stdout=DEVNULL, stderr=DEVNULL)
return res
# Send email when server is down
def sendEmail(serverName):
body = MIMEText("Server " + serverName + " is down. Please check!", "plain")
email = MIMEMultipart()
email.attach(body)
email["From"] = "Notification <[email protected]>"
email["To"] = "[email protected]"
email["Subject"] = "Alert! Server down!!!"
try:
# Login to Gmail and send emails using above senders and receivers
serverSSL = smtplib.SMTP_SSL("smtp.gmail.com", 465)
serverSSL.ehlo()
serverSSL.login(sender, senderPass)
# SSL does not support TLS, no need to call serverSSL.starttls()
serverSSL.sendmail(sender, receiver, email.as_string())
serverSSL.close()
except:
print "Error sending email"
############## APP ROUTES ################
# This route is for homepage, always displays all monitored servers
@app.route('/')
def index():
cur = g.db.execute("select * from servers order by name")
serverList = [dict(id=row[0], serverName=row[1], status=isAlive(row[1])) for row in cur.fetchall()]
for server in serverList:
if server['status'] == 1:
sendEmail(server['serverName'])
return render_template("index.html", serverList=serverList)
# Login page
@app.route('/login', methods=['GET', 'POST'])
def login():
# If method = POST, validate username & password then decide
error = None
if request.method == 'POST':
cur = g.db.execute("select * from users")
# Get all users and convert the result to a dictionary, otherwise a tuple
userList = [dict(username=row[0], password=row[1]) for row in cur.fetchall()]
inputUser = request.form['username']
inputPass = request.form['password']
usernameList = [u.get('username') for u in userList]
if inputUser not in usernameList:
error = "Username does not exist"
else:
for user in userList:
if user['username'] == inputUser:
if user['password'] != inputPass:
error = "Incorrect password"
else:
session['logged_in'] = True
flash('You are logged in')
return redirect(url_for('index'))
# If method = GET, simply displays login page
return render_template('login.html', error=error)
# Logout
@app.route('/logout')
def logout():
# Clear the 'logged_in' key to log out
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('index'))
# Add new server to monitor
@app.route('/addServer', methods=['POST'])
def add_server():
if not session.get('logged_in'):
abort(401)
g.db.execute("insert into servers (name) values (?)",
[request.form['name']])
g.db.commit()
return redirect(url_for('index'))
# Remove a server to stop monitoring
@app.route('/removeServer/<serverId>', methods=['POST'])
def remove_server(serverId):
if not session.get('logged_in'):
abort(401)
g.db.execute("delete from servers where id = ?", [serverId])
g.db.commit()
return redirect(url_for('index'))
# Viewing/Creating new users
@app.route('/addUser', methods=['GET', 'POST'])
def add_user():
if not session.get('logged_in'):
abort(401)
# If method is GET, display all users
cur = g.db.execute("select * from users")
userList = [dict(username=row[0], password=row[1]) for row in cur.fetchall()]
usernameList = [u.get('username') for u in userList]
# otherwise, add new user to system
error = None
if request.method == 'POST':
inputUser = request.form['username']
# If username entered already in the database
if inputUser in usernameList:
error = "User already exists!"
else: # otherwise, add new user
g.db.execute("insert into users (username, password) values (?, ?)",
[request.form['username'], request.form['password']])
g.db.commit()
return redirect(url_for('add_user'))
return render_template('addUser.html', userList=userList, error=error)
# This route is used to remove an existing user
@app.route('/removeUser/<username>', methods=['POST'])
def remove_user(username):
if not session.get('logged_in'):
abort(401)
g.db.execute("delete from users where username = ?", [username])
g.db.commit()
return redirect(url_for('add_user'))
if __name__ == "__main__":
app.run()