public
anonymous / indicator-sysload.c
Last active

indicator-sysload is an Ubuntu appindicator that displays the system load (CPU, memory usage, network traffic).

  • Download Gist
indicator-sysload.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/*
 
This is an Ubuntu appindicator that displays the system load
(CPU, memory usage, network traffic).
 
It looks like this: http://imgur.com/W4PCc .
 
Compile with:
gcc -Wall `pkg-config --cflags --libs gtk+-2.0 appindicator-0.1 libgtop-2.0` -o indicator-sysload ./indicator-sysload.c
 
WARNING:
 
The periodic update makes Compiz leak memory.
12-24 hours of running will cause performance
issues, and you have to restart Gnome.
 
License: this software is in the public domain.
 
*/
 
#include <gtk/gtk.h>
#include <libappindicator/app-indicator.h>
#include <glibtop.h>
#include <glibtop/cpu.h>
#include <glibtop/mem.h>
#include <glibtop/netload.h>
#include <glibtop/netlist.h>
 
/* update period in seconds */
int period = 1;
 
AppIndicator *indicator;
GtkWidget *indicator_menu;
 
GtkWidget *cpu_item;
GtkWidget *memory_item;
GtkWidget *net_down_item;
GtkWidget *net_up_item;
GtkWidget *sysmon_item;
GtkWidget *quit_item;
 
int get_cpu()
{
static int cpu_total_old = 0;
static int cpu_idle_old = 0;
 
glibtop_cpu cpu;
glibtop_get_cpu (&cpu);
int diff_total = cpu.total - cpu_total_old;
int diff_idle = cpu.idle - cpu_idle_old;
 
cpu_total_old = cpu.total;
cpu_idle_old = cpu.idle;
 
return 100 * (diff_total-diff_idle) / diff_total;
}
 
int get_memory(void)
{
glibtop_mem mem;
glibtop_get_mem (&mem);
/* used memory in percents */
return 100 - 100 * (mem.free + mem.buffer + mem.cached) / mem.total;
}
 
gchar* format_net_label(gchar *prefix, int data)
{
gchar *string;
if(data < 1000)
{
string = g_strdup_printf("%s%d B/s", prefix, data);
}
else if(data < 1000000)
{
string = g_strdup_printf("%s%.1f KiB/s", prefix, data/1000.0);
}
else
{
string = g_strdup_printf("%s%.2f MiB/s", prefix, data/1000000.0);
}
return string;
}
 
void get_net(int traffic[2])
{
static int bytes_in_old = 0;
static int bytes_out_old = 0;
static gboolean first_run = TRUE;
glibtop_netload netload;
glibtop_netlist netlist;
int bytes_in = 0;
int bytes_out = 0;
int i = 0;
gchar **interfaces = glibtop_get_netlist(&netlist);
for(i = 0; i < netlist.number; i++)
{
glibtop_get_netload(&netload, interfaces[i]);
bytes_in += netload.bytes_in;
bytes_out += netload.bytes_out;
}
g_strfreev(interfaces);
if(first_run)
{
bytes_in_old = bytes_in;
bytes_out_old = bytes_out;
first_run = FALSE;
}
 
traffic[0] = (bytes_in - bytes_in_old) / period;
traffic[1] = (bytes_out - bytes_out_old) / period;
bytes_in_old = bytes_in;
bytes_out_old = bytes_out;
}
 
gboolean update()
{
int cpu = get_cpu();
int memory = get_memory();
int net_traffic[2] = {0, 0};
get_net(net_traffic);
int net_down = net_traffic[0];
int net_up = net_traffic[1];
static const gchar *separator = "|";
static const gchar *nothing = " ";
/* the half and full block doesn't fit (at least in my system, perhaps a font substitution issue)*/
/*static const gchar *levels[] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};*/
static const gchar *levels[] = {"▁", "▂", "▃", "▅", "▆", "▇"};
int level_num = sizeof(levels) / sizeof("▂");
 
int cpu_max = 100;
int mem_max = 100;
static int net_down_max = 0;
static int net_up_max = 0;
int weight = 4;
net_down_max = MAX(net_down, (weight*net_down_max + net_down) / (weight+1));
net_up_max = MAX(net_up, (weight*net_up_max + net_up) / (weight+1));
gchar *cpu_level = (gchar *) ((cpu == 0) ? nothing : levels[level_num * cpu / (cpu_max+1)]);
gchar *memory_level = (gchar *) ((memory == 0) ? nothing : levels[level_num * memory / (mem_max+1)]);
gchar *net_down_level = (gchar *) ((net_down == 0) ? nothing : levels[level_num * net_down / (net_down_max+1)]);
gchar *net_up_level = (gchar *) ((net_up == 0) ? nothing : levels[level_num * net_up / (net_up_max+1)]);
gchar *indicator_label = g_strconcat( separator
, cpu_level
, memory_level
, net_down_level
, net_up_level
, separator
, NULL);
 
static const gchar *label_guide = "|▁▂▃▅|";
app_indicator_set_label(indicator, indicator_label, label_guide);
g_free(indicator_label);
 
gchar *cpu_label = g_strdup_printf("CPU: %d%%", cpu);
gtk_menu_item_set_label(GTK_MENU_ITEM(cpu_item), cpu_label);
g_free(cpu_label);
gchar *memory_label = g_strdup_printf("Memory: %d%%", memory);
gtk_menu_item_set_label(GTK_MENU_ITEM(memory_item), memory_label);
g_free(memory_label);
gchar *net_down_label = format_net_label("Net ↓: ", net_down);
gtk_menu_item_set_label(GTK_MENU_ITEM(net_down_item), net_down_label);
g_free(net_down_label);
 
gchar *net_up_label = format_net_label("Net ↑: ", net_up);
gtk_menu_item_set_label(GTK_MENU_ITEM(net_up_item), net_up_label);
g_free(net_up_label);
return TRUE;
}
 
static void start_sysmon(GtkWidget *widget, gpointer data)
{
GError *err = NULL;
gchar *argv[] = {"gnome-system-monitor"};
if( ! g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &err))
{
fprintf(stderr, "Error: %s\n", err->message);
}
}
 
int main (int argc, char **argv)
{
gtk_init (&argc, &argv);
 
indicator_menu = gtk_menu_new();
cpu_item = gtk_image_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), cpu_item);
 
memory_item = gtk_image_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), memory_item);
 
net_down_item = gtk_image_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), net_down_item);
 
net_up_item = gtk_image_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), net_up_item);
GtkWidget *sep = gtk_separator_menu_item_new();
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), sep);
 
sysmon_item = gtk_menu_item_new_with_label("Gnome System Monitor");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), sysmon_item);
g_signal_connect(sysmon_item, "activate", G_CALLBACK (start_sysmon), NULL);
 
/* I don't think it's necessary, uncomment if you want it*/
 
/* quit_item = gtk_menu_item_new_with_label("Quit");*/
/* gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), quit_item);*/
/* g_signal_connect(quit_item, "activate", G_CALLBACK (gtk_main_quit), NULL);*/
 
gtk_widget_show_all(indicator_menu);
 
indicator = app_indicator_new ("sysload", "", APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
app_indicator_set_label(indicator, "sysload", "sysload");
app_indicator_set_menu(indicator, GTK_MENU (indicator_menu));
update();
/* update period in milliseconds */
g_timeout_add(1000*period, (GtkFunction) update, NULL);
 
gtk_main ();
 
return 0;
}

You need to first install some packages:

sudo apt-get install libgtop2-dev libappindicator-dev libgtk2.0-dev

And the compile command above is wrong. It won't link properly. You have to put indicator-sysload.c before the pkg-config command, like this:

gcc -o indicator-sysload indicator-sysload.c -Wall `pkg-config --cflags --libs gtk+-2.0 appindicator-0.1 libgtop-2.0`

Then there is a bug on line 137. It will cause the program to crash when the load gets too high. Change to:

int level_num = sizeof(levels) / sizeof(levels[0]);

You are better off just using my fork: https://gist.github.com/stefansundin/5792671
I also made it use all of the blocks, as all of them worked for me (this gist is 2 years old). I also removed the space when the load is zero, since that caused the label to bounce around too much to my liking (the width of a block != width of a space).

Enjoy!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.