-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gmemstr/three-point-oh
2.1.0
- Loading branch information
Showing
7 changed files
with
170 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.