Added pastafariagent
This commit is contained in:
parent
b1698487ca
commit
f08c99800d
3 changed files with 201 additions and 92 deletions
102
pastafaristats/pastafariagent.py
Normal file
102
pastafaristats/pastafariagent.py
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
from PyQt5.QtGui import *
|
||||||
|
from PyQt5.QtWidgets import *
|
||||||
|
from PyQt5.QtCore import *
|
||||||
|
from pastafaristats import send_info_daemon
|
||||||
|
from time import sleep
|
||||||
|
import os
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
def quit_app():
|
||||||
|
print('Quitting Application...')
|
||||||
|
#print(QThreadPool.globalInstance().activeThreadCount())
|
||||||
|
#QThreadPool.globalInstance().exit()
|
||||||
|
"""
|
||||||
|
thread=QThread.currentThread()
|
||||||
|
print(thread)
|
||||||
|
thread.quit()
|
||||||
|
"""
|
||||||
|
#QThreadPool.globalInstance().tryTake(p)
|
||||||
|
app.quit()
|
||||||
|
|
||||||
|
# Get pid
|
||||||
|
|
||||||
|
pid=os.getpid()
|
||||||
|
|
||||||
|
p=psutil.Process(pid)
|
||||||
|
|
||||||
|
p.terminate()
|
||||||
|
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
# Killing using pid
|
||||||
|
|
||||||
|
#p.stop()
|
||||||
|
|
||||||
|
def send_data():
|
||||||
|
print('Init daemon...')
|
||||||
|
while True:
|
||||||
|
|
||||||
|
send_info_daemon.run()
|
||||||
|
|
||||||
|
sleep(120)
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessRunnable(QRunnable):
|
||||||
|
|
||||||
|
def __init__(self, target, args):
|
||||||
|
QRunnable.__init__(self)
|
||||||
|
#self.setAutoDelete(True)
|
||||||
|
#self.waitForDone(100)
|
||||||
|
self.t = target
|
||||||
|
self.args = args
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.t(*self.args)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
#QThreadPool.globalInstance().waitForDone(100)
|
||||||
|
QThreadPool.globalInstance().start(self)
|
||||||
|
"""
|
||||||
|
def stop(self):
|
||||||
|
print('Stopping daemon...')
|
||||||
|
del self.t
|
||||||
|
"""
|
||||||
|
|
||||||
|
app = QApplication([])
|
||||||
|
app.setQuitOnLastWindowClosed(False)
|
||||||
|
|
||||||
|
# Adding an icon
|
||||||
|
icon = QIcon("icon.png")
|
||||||
|
|
||||||
|
# Adding item on the menu bar
|
||||||
|
tray = QSystemTrayIcon()
|
||||||
|
tray.setIcon(icon)
|
||||||
|
tray.setVisible(True)
|
||||||
|
|
||||||
|
# Creating the options
|
||||||
|
|
||||||
|
menu = QMenu()
|
||||||
|
|
||||||
|
option1 = QAction("Pastafari monitoring")
|
||||||
|
menu.addAction(option1)
|
||||||
|
"""
|
||||||
|
option2 = QAction("GFG")
|
||||||
|
menu.addAction(option2)
|
||||||
|
"""
|
||||||
|
|
||||||
|
# To quit the app
|
||||||
|
quit = QAction("Quit")
|
||||||
|
quit.triggered.connect(quit_app)
|
||||||
|
menu.addAction(quit)
|
||||||
|
|
||||||
|
# Adding options to the System Tray
|
||||||
|
tray.setContextMenu(menu)
|
||||||
|
|
||||||
|
# Begin background process
|
||||||
|
|
||||||
|
p = ProcessRunnable(target=send_data, args=())
|
||||||
|
p.start()
|
||||||
|
|
||||||
|
# Begin tray loop
|
||||||
|
|
||||||
|
app.exec_()
|
||||||
|
|
@ -16,100 +16,107 @@ import signal
|
||||||
|
|
||||||
# Get config from /etc/pastafari or ~/.config/pastafari
|
# Get config from /etc/pastafari or ~/.config/pastafari
|
||||||
|
|
||||||
|
# Load configuration
|
||||||
|
|
||||||
|
yes_config=False
|
||||||
|
|
||||||
|
user_home=str(Path.home())
|
||||||
|
|
||||||
|
config = configparser.ConfigParser(interpolation=None)
|
||||||
|
|
||||||
|
if os.path.isfile(user_home+'/.config/pastafari/stats.cfg'):
|
||||||
|
|
||||||
|
config.read(user_home+'/.config/pastafari/stats.cfg')
|
||||||
|
|
||||||
|
yes_config=True
|
||||||
|
|
||||||
|
elif os.path.isfile('/etc/pastafari/stats.cfg'):
|
||||||
|
|
||||||
|
config.read('/etc/pastafari/stats.cfg')
|
||||||
|
|
||||||
|
yes_config=True
|
||||||
|
|
||||||
|
|
||||||
|
if not yes_config:
|
||||||
|
|
||||||
|
exit("Sorry, cannot load config file")
|
||||||
|
|
||||||
|
if not 'DEFAULT' in config:
|
||||||
|
|
||||||
|
exit("Sorry, config file need [DEFAULT] section")
|
||||||
|
|
||||||
|
if not 'url_server' in config['DEFAULT']:
|
||||||
|
|
||||||
|
exit("Sorry, config file need url_server variable in [DEFAULT] section")
|
||||||
|
|
||||||
|
url=config['DEFAULT']['url_server']
|
||||||
|
|
||||||
|
def run():
|
||||||
|
|
||||||
|
network_info=psutil.net_io_counters(pernic=False)
|
||||||
|
|
||||||
|
network_devices=psutil.net_if_addrs()
|
||||||
|
|
||||||
|
cpu_idle=psutil.cpu_percent(interval=1)
|
||||||
|
|
||||||
|
cpus_idle=psutil.cpu_percent(interval=1, percpu=True)
|
||||||
|
|
||||||
|
cpu_number=psutil.cpu_count()
|
||||||
|
|
||||||
|
disk_info=psutil.disk_partitions()
|
||||||
|
|
||||||
|
partitions={}
|
||||||
|
|
||||||
|
for disk in disk_info:
|
||||||
|
|
||||||
|
partition=str(disk[1])
|
||||||
|
partitions[partition]=psutil.disk_usage(partition)
|
||||||
|
#print(partition+"="+str(partitions[partition]))
|
||||||
|
|
||||||
|
dev_info={}
|
||||||
|
|
||||||
|
mem_info=psutil.virtual_memory()
|
||||||
|
|
||||||
|
json_info=json.dumps({'net_info': network_info, 'cpu_idle': cpu_idle, 'cpu_number': cpu_number, 'disks_info': partitions, 'mem_info': mem_info})
|
||||||
|
|
||||||
|
data = urllib.parse.urlencode({'data_json': json_info})
|
||||||
|
|
||||||
|
data = data.encode('ascii')
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
if url[:5]=='https':
|
||||||
|
|
||||||
|
# For nexts versions 3.5
|
||||||
|
|
||||||
|
ctx = ssl.create_default_context()
|
||||||
|
ctx.check_hostname = False
|
||||||
|
ctx.verify_mode = ssl.CERT_NONE
|
||||||
|
|
||||||
|
content=urllib.request.urlopen(url, data, context=ctx)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
content=urllib.request.urlopen(url, data)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print('Cannot connect to data server -> '+str(e))
|
||||||
|
|
||||||
|
def start():
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
run()
|
||||||
|
|
||||||
|
sleep(120)
|
||||||
|
|
||||||
def catch_signal(sig, frame):
|
def catch_signal(sig, frame):
|
||||||
print('Exiting...')
|
print('Exiting...')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, catch_signal)
|
signal.signal(signal.SIGINT, catch_signal)
|
||||||
|
|
||||||
def start():
|
|
||||||
|
|
||||||
yes_config=False
|
|
||||||
|
|
||||||
user_home=str(Path.home())
|
|
||||||
|
|
||||||
config = configparser.ConfigParser(interpolation=None)
|
|
||||||
|
|
||||||
if os.path.isfile(user_home+'/.config/pastafari/stats.cfg'):
|
|
||||||
|
|
||||||
config.read(user_home+'/.config/pastafari/stats.cfg')
|
|
||||||
|
|
||||||
yes_config=True
|
|
||||||
|
|
||||||
elif os.path.isfile('/etc/pastafari/stats.cfg'):
|
|
||||||
|
|
||||||
config.read('/etc/pastafari/stats.cfg')
|
|
||||||
|
|
||||||
yes_config=True
|
|
||||||
|
|
||||||
|
|
||||||
if not yes_config:
|
|
||||||
|
|
||||||
exit("Sorry, cannot load config file")
|
|
||||||
|
|
||||||
if not 'DEFAULT' in config:
|
|
||||||
|
|
||||||
exit("Sorry, config file need [DEFAULT] section")
|
|
||||||
|
|
||||||
if not 'url_server' in config['DEFAULT']:
|
|
||||||
|
|
||||||
exit("Sorry, config file need url_server variable in [DEFAULT] section")
|
|
||||||
|
|
||||||
url=config['DEFAULT']['url_server']
|
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
network_info=psutil.net_io_counters(pernic=False)
|
|
||||||
|
|
||||||
network_devices=psutil.net_if_addrs()
|
|
||||||
|
|
||||||
cpu_idle=psutil.cpu_percent(interval=1)
|
|
||||||
|
|
||||||
cpu_number=psutil.cpu_count()
|
|
||||||
|
|
||||||
disk_info=psutil.disk_partitions()
|
|
||||||
|
|
||||||
partitions={}
|
|
||||||
|
|
||||||
for disk in disk_info:
|
|
||||||
|
|
||||||
partition=str(disk[1])
|
|
||||||
partitions[partition]=psutil.disk_usage(partition)
|
|
||||||
#print(partition+"="+str(partitions[partition]))
|
|
||||||
|
|
||||||
dev_info={}
|
|
||||||
|
|
||||||
mem_info=psutil.virtual_memory()
|
|
||||||
|
|
||||||
json_info=json.dumps({'net_info': network_info, 'cpu_idle': cpu_idle, 'cpu_number': cpu_number, 'disks_info': partitions, 'mem_info': mem_info})
|
|
||||||
|
|
||||||
data = urllib.parse.urlencode({'data_json': json_info})
|
|
||||||
|
|
||||||
data = data.encode('ascii')
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
if url[:5]=='https':
|
|
||||||
|
|
||||||
# For nexts versions 3.5
|
|
||||||
|
|
||||||
ctx = ssl.create_default_context()
|
|
||||||
ctx.check_hostname = False
|
|
||||||
ctx.verify_mode = ssl.CERT_NONE
|
|
||||||
|
|
||||||
content=urllib.request.urlopen(url, data, context=ctx)
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
content=urllib.request.urlopen(url, data)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
|
|
||||||
print('Cannot connect to data server -> '+str(e))
|
|
||||||
|
|
||||||
|
|
||||||
sleep(120)
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
|
|
||||||
start()
|
start()
|
||||||
|
|
|
||||||
8
setup.py
8
setup.py
|
|
@ -14,13 +14,13 @@ if sys.version_info < (3, 5):
|
||||||
|
|
||||||
setup(name='pastafaristats',
|
setup(name='pastafaristats',
|
||||||
version='1.0.0',
|
version='1.0.0',
|
||||||
description='Simple scripts for send basic data of a server how complement to other stats solutions more comples.',
|
description='Simple scripts for send basic data of a server how complement to other stats solutions more complex.',
|
||||||
author='Antonio de la Rosa Caballero',
|
author='Antonio de la Rosa Caballero',
|
||||||
author_email='antonio.delarosa@coesinfo.com',
|
author_email='antonio.delarosa@coesinfo.com',
|
||||||
url='https://bitbucket.org/paramecio/pastafaristats/',
|
url='https://bitbucket.org/paramecio/pastafaristats/',
|
||||||
packages=['pastafaristats'],
|
packages=['pastafaristats'],
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
install_requires=['psutil'],
|
install_requires=['psutil', 'pyqt5'],
|
||||||
entry_points={'console_scripts': [
|
entry_points={'console_scripts': [
|
||||||
'pastafaristats = pastafaristats.send_info_daemon:start',
|
'pastafaristats = pastafaristats.send_info_daemon:start',
|
||||||
]},
|
]},
|
||||||
|
|
@ -31,9 +31,9 @@ setup(name='pastafaristats',
|
||||||
'Intended Audience :: Developers',
|
'Intended Audience :: Developers',
|
||||||
'License :: OSI Approved :: GPLV3 License',
|
'License :: OSI Approved :: GPLV3 License',
|
||||||
'Programming Language :: Python :: 3',
|
'Programming Language :: Python :: 3',
|
||||||
'Programming Language :: Python :: 3.5',
|
|
||||||
'Programming Language :: Python :: 3.6',
|
'Programming Language :: Python :: 3.6',
|
||||||
'Programming Language :: Python :: 3.7',
|
'Programming Language :: Python :: 3.7',
|
||||||
'Programming Language :: Python :: 3.8'
|
'Programming Language :: Python :: 3.8',
|
||||||
|
'Programming Language :: Python :: 3.9'
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue