--Here is a summary of desktop notifications using python. --It can be easily implemented by using the python library. ――The following is a usage example (it is just an example that it may be used in this way, and I think it can be used for more general purposes) --Notify when the battery level of a certain product is less than xx% of the battery level, assuming that there is a database that stores it in real time. --Notify when the memory usage of a server exceeds 80%, assuming that there is a database that stores the memory usage in real time. ――I think that using it well will help improve work efficiency.
--By executing the following source code, you can achieve the following. --Every 10 seconds, SQL is issued to the DB table to get the value, and if the threshold is exceeded, the desktop is notified. ――Since this is an example, it is a simple table that contains the following time and data, but I think that it can be expanded in various forms such as using View etc. to make the data averaged for 1 hour.
from plyer import notification
import psycopg2
import schedule
import time
def job(cur):
alert = ""
cur.execute("select data from sample order by time desc limit 1;")
rows = cur.fetchall()
hoge = rows[0][0]
if int(hoge) >= 98:
alert += "hoge is 100%Is approaching."
if alert != "":
notification.notify(
title = "warning",
message = alert,
app_name = "Monitor monitoring"
)
print("Start monitoring.\n A desktop notification will be sent when the reference value is reached.")
#Connection information creation
con = psycopg2.connect("host=xxx.xxx.xxx.xxx port=xxxx dbname=xxxx user=xxxx password=xxxx")
cur = con.cursor()
#Execute job every specified time (every 10 seconds this time)
schedule.every(10).seconds.do(job, cur)
while True:
try:
schedule.run_pending()
time.sleep(1)
except KeyboardInterrupt:
print("It was interrupted.")
cur.close()
con.close()
break
--The following desktop notifications are sent when the threshold is exceeded.
Recommended Posts