Skip to content

Commit

Permalink
Merge pull request #3 from gmemstr/three-point-oh
Browse files Browse the repository at this point in the history
2.1.0
  • Loading branch information
gmemstr authored Mar 13, 2017
2 parents 35bb828 + 2f9b38e commit ced4a76
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 200 deletions.
24 changes: 11 additions & 13 deletions App.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from multiprocessing import Process
from threading import Thread
from src.Webserver import Webserver
from src.Slackbot import Bot
from src.Config import Config
from src.Statuses import Scanning
from src.Scan import Scan

config = Config()
ws = Webserver()
scn = Scanning()
scn = Scan()
sb = Bot()

if __name__ == "__main__":
Expand All @@ -22,22 +22,20 @@
# The order in which the processes are started
# is important, since the flask process effectively
# blocks the rest of the code from running (wtfkwbtihiw)
scnp = Process(target=scn.Loop()).start()
sbp = Process(target=sb.Loop()).start()
wsp = Process(target=ws.Run()).start()
scnp = Thread(target=scn.Fetch().start())
sbp = Thread(target=sb.Loop()).start()
wsp = Thread(target=ws.Run()).start()

elif config.Get("enable_webserver") is True:
print("Webserver starting up")
scnp = Process(target=scn.Loop()).start()
wsp = Process(target=ws.Run()).start()

#scnp = Thread(target=scn.Loop()).start()
wsp = Thread(target=ws.Run()).start()

elif config.Get("enable_slackbot") is True:
print("Slackbot enabled")
scnp = Process(target=scn.Loop()).start()
sbp = Process(target=sb.Loop()).start()
scnp = Thread(target=scn.Loop()).start()
sbp = Thread(target=sb.Loop()).start()

else:
print("No frontends enabed. Scanning to cache only.")
scn.Loop()

109 changes: 0 additions & 109 deletions src/Cache.py

This file was deleted.

74 changes: 74 additions & 0 deletions src/SQL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import asyncio
import MySQLdb
from src.Config import Config

class Sql:
def __init__(self):
self.Config = Config()

self.config = Config()
self.sqluser = self.config.Get("sql_user")
self.sqlpass = self.config.Get("sql_pass")
self.sqltable = self.config.Get("sql_db")

self.db = MySQLdb.connect(user=self.sqluser,passwd=self.sqlpass,db="platypus")
self.c = self.db.cursor()

def CheckConnection(self):
try:
self.db.ping()
except:
self.db = MySQLdb.connect(user=self.sqluser,passwd=self.sqlpass,db="platypus")

def Get(self,filter=None,panel=None):
self.CheckConnection()
if filter == None:
self.c.execute("SELECT * FROM " + self.sqltable)
return self.c.fetchall()
if filter == "one":
self.c.execute("SELECT * FROM " + self.sqltable + " WHERE id="+panel)
return self.c.fetchall()
else:
raise ValueError('Invalid filter for SQL query')

def Set(self,panel,online,cpu,memory,disk):
self.CheckConnection()
self.c.execute("UPDATE " + self.sqltable + " SET online=%s,cpu=%s,memory=%s,disk=%s WHERE id=%s",
(online, cpu, memory, disk, panel))
self.db.commit()

def RemoveServer(self, panel):
self.CheckConnection()

self.c.execute("DELETE FROM "+self.sqltable+" WHERE id="+str(panel))
self.db.commit()
return True
def CreateServer(self, panel, form):
self.CheckConnection()

# Insert new server into database
self.c.execute("INSERT INTO " + self.sqltable + " (id,name,hostname,location) VALUES (%s,%s,%s,%s)",
(int(form['id']),form['name'],form['hostname'],form['location']))
self.db.commit()
return True
def ModServer(self, panel, form):
self.CheckConnection()

# Edit server
self.c.execute("UPDATE " + self.sqltable + " SET name=%s,hostname=%s WHERE id=%s",
(form['name'], form['hostname'], panel))
self.db.commit()
return True

def GetAsJson(self,panel):
raw = self.Get("one",panel)
res = {}
for s in raw:
print(s)
res[s[0]] = {"name": s[1],
"online": s[4],
"location": s[3],
"cpu": s[6],
"memory":s[7],
"disk":s[8]}
return res
65 changes: 65 additions & 0 deletions src/Scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import requests
import threading
# from src.Cache import Handler
from src.SQL import Sql
import time
from src.Config import Config

config = Config()
sql = Sql()

class Scan:
def __init__(self):
self.panels = sql.Get()

def Fetch(self,panel=None):
print("Fetching panels")
if panel == None:
for p in self.panels:
result = self.Check(p)
print(result)
sql.Set(p[0],
result['online'],
str(result['cpu']),
str(result['memory']),
str(result['disk']))

else:
panel = sql.Get("one", panel)[0]
print(panel)
return self.Check(panel)

def Check(self, panel):
id = panel[0]

try:
request = requests.get("http://" + panel[2] + config.Get("stats_path"),
timeout = config.Get("scan_timeout"))

print(panel[0], "online", request.status_code)
if request.status_code == 404:
return {"name": panel[1],
"online": True,
"cpu": 0,
"memory": 0,
"disk": 0}
else:
data = request.json()
return {"name": panel[1],
"online": True,
"cpu": data["cpu"],
"memory": data["memory"],
"disk": data["hdd"]}

except Exception as e:
print(panel[0], "offline")
print(e)
return {"name": panel[1],
"online": False,
"cpu": 0,
"memory": 0,
"disk": 0}

def Loop(self):
self.Fetch()
threading.Timer(config.Get("scan_interval"), self.Loop).start()
21 changes: 11 additions & 10 deletions src/Slackbot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from slackclient import SlackClient
from src.Cache import Handler
from src.SQL import Sql
from src.Config import Config
import threading

config = Config()
channel = config.Get("slack_channel")
token = config.Get("slack_api_key")
sc = SlackClient(token)
handler = Handler()
sql = Sql()

class Bot:
def Post(self,message, channel, username, icon):
Expand All @@ -16,28 +16,29 @@ def Post(self,message, channel, username, icon):
username=username, icon_emoji=icon)


def BuildMessage(self,data):
def ServerReport(self,data):
servers = sql.Get()
post = True
off = 0
channel = config.Get("slack_channel")
username = "Platypus"
icon = ":desktop_computer:"
message = "Some panels may be offline!"
for s in data:

for s in servers:
if s[4] == 0:
message = message + " " + s[1] + " (" + s[2] + ")"
off = off + 1

if off > 1: post = True
else: post = False

if post is True: self.Post(message, channel, username, icon)
if off >= 2: icon = ":exclamation:"
if off >= 4: icon = ":fire:"

username = "Platypus (" + str(off) + ")"
if post is True: self.Post(message, channel, username, icon)

def Data(self):
data = handler.Get()
self.BuildMessage(data)

def Loop(self):
self.Data()
self.Fetch()
threading.Timer(config.Get("slack_interval"), self.Loop).start()
Loading

0 comments on commit ced4a76

Please sign in to comment.