Last active
November 22, 2019 00:23
-
-
Save atilberk/71e372eb6fb4fee45d32 to your computer and use it in GitHub Desktop.
An CPU/GPU temperature indicator in for Ubuntu
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# I am using this to monitor my cpu's temperature in the top bar of my os | |
# I have tested the script on Ubuntu 15.10 with Python 2.7.10 | |
# Update: I used to require acpi but then found a simpler way | |
import sys,time,re,commands,os | |
from gi.repository import Gtk, GLib | |
from gi.repository import AppIndicator3 as appindicator | |
class MessageDialogWindow(Gtk.Window): | |
def __init__(self): | |
Gtk.Window.__init__(self, title="Temp-Indicator") | |
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Temp-Indicator cannot reach the data") | |
dialog.format_secondary_text("Cannot find the command \"acpi\" in your PATH. Please install acpi (sudo apt-get install acpi) or check your PATH then try again.") | |
dialog.run() | |
dialog.destroy() | |
sys.exit(0) | |
class TempIndicator: | |
critical_threshold = 65.0 | |
notify_threshold = 70.0 | |
refresh_normal = 5 | |
refresh_critical = 2 | |
def __init__(self): | |
s = commands.getstatusoutput("which acpi")[1] | |
if (s == ""): | |
win = MessageDialogWindow() | |
win.connect("delete-event", Gtk.main_quit) | |
win.show_all() | |
else: | |
icon_image = "/usr/share/unity/icons/panel-shadow.png" | |
self.ind = appindicator.Indicator.new("Temp",icon_image,appindicator.IndicatorCategory.APPLICATION_STATUS) | |
self.ind.set_status(appindicator.IndicatorStatus.ACTIVE) # | |
self.refresh_rate = TempIndicator.refresh_normal # Refresh interval in seconds | |
self.measure = True | |
self.menu_structure() | |
# Menu structure | |
def menu_structure(self): | |
updated = time.asctime() | |
s = commands.getstatusoutput("find /sys/class/thermal/thermal_zone* | xargs -I % cat %/temp | sort -r | head -1") | |
#temps = re.findall(r"[0-9]+.[0-9]+", s[1]) | |
#temp = max(map(float,temps)) | |
temp = float(s[1])/1000; | |
# GTK menu | |
self.menu = Gtk.Menu() | |
self.menu_updated = Gtk.MenuItem("Updated: "+updated) | |
self.menu_toggle = Gtk.MenuItem(("Pause" if self.measure else "Resume")) | |
self.menu_toggle.connect("activate", self.toggle) | |
self.separator = Gtk.SeparatorMenuItem() | |
self.exit = Gtk.MenuItem("Quit") | |
self.exit.connect("activate", self.quit) | |
#show menu | |
self.menu_updated.show() | |
self.menu_toggle.show() | |
self.separator.show() | |
self.exit.show() | |
# Append menu | |
self.menu.append(self.menu_updated) | |
self.menu.append(self.menu_toggle) | |
self.menu.append(self.separator) | |
self.menu.append(self.exit) | |
self.ind.set_menu(self.menu) | |
# Set label in celcius temperature | |
self.ind.set_label(str(temp)+u"\u2103".encode('utf-8'),"") | |
# Notify when temp gets too high | |
if float(temp) > TempIndicator.notify_threshold: | |
os.system('notify-send "TempIndicator" "Core temperature is high!"') | |
# Refresh indicator | |
self.refresh_rate = TempIndicator.refresh_normal if float(temp) < TempIndicator.critical_threshold else TempIndicator.refresh_critical | |
if self.measure: | |
GLib.timeout_add_seconds(self.refresh_rate,self.menu_structure) | |
def toggle(self,widget): | |
self.measure = not self.measure | |
self.menu_structure() | |
def quit(self, widget): | |
sys.exit(0) | |
if __name__ == "__main__": | |
indicator = TempIndicator() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment