Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions plugins/cpu/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* and was extended by Eygene Ryabinkin <[email protected]>
*/

#include <unistd.h>
#include <string.h>
#include "misc.h"
#include "../chart/chart.h"
Expand All @@ -29,6 +30,7 @@ typedef struct {
struct cpu_stat cpu_prev;
int timer;
gchar *colors[1];
unsigned int core;
} cpu_priv;

static chart_class *k;
Expand All @@ -38,22 +40,36 @@ static void cpu_destructor(plugin_instance *p);

#if defined __linux__
static int
cpu_get_load_real(struct cpu_stat *cpu)
cpu_get_load_real(struct cpu_stat *cpu, unsigned int core)
{
FILE *stat;

gchar buf[40];
memset(cpu, 0, sizeof(struct cpu_stat));
stat = fopen("/proc/stat", "r");
if(!stat)
return -1;
if (fscanf(stat, "cpu %lu %lu %lu %lu %lu", &cpu->u, &cpu->n, &cpu->s,
&cpu->i, &cpu->w));
fclose(stat);
if (core == 0)
{
stat = fopen("/proc/stat", "r");
if(!stat)
return -1;
if (fscanf(stat, "cpu %lu %lu %lu %lu %lu", &cpu->u, &cpu->n, &cpu->s,
&cpu->i, &cpu->w));
fclose(stat);
}
else
{
DBG("buf=%s\n", buf);
sprintf(buf, "%s%u %s", "/bin/grep cpu", core-1, "/proc/stat");
stat = popen(buf, "r");
if (!stat)
return -1;
sprintf(buf, "%s%u %s", "cpu", core-1, "%lu %lu %lu %lu %lu");
if (fscanf(stat, buf, &cpu->u, &cpu->n, &cpu->s,&cpu->i, &cpu->w));
pclose(stat);
}
return 0;
}
#elif defined __FreeBSD__
static int
cpu_get_load_real(struct cpu_stat *cpu)
cpu_get_load_real(struct cpu_stat *cpu, const unsigned int core)
{
static int mib[2] = { -1, -1 }, init = 0;
size_t j;
Expand Down Expand Up @@ -81,13 +97,13 @@ cpu_get_load_real(struct cpu_stat *cpu)
}
#else
static int
cpu_get_load_real(struct cpu_stat *s)
cpu_get_load_real(struct cpu_stat *s, const unsigned int core)
{
memset(cpu, 0, sizeof(struct cpu_stat));
return 0;
}
#endif

static int
cpu_get_load(cpu_priv *c)
{
Expand All @@ -100,8 +116,8 @@ cpu_get_load(cpu_priv *c)
memset(&cpu, 0, sizeof(cpu));
memset(&cpu_diff, 0, sizeof(cpu_diff));
memset(&total, 0, sizeof(total));

if (cpu_get_load_real(&cpu))
if (cpu_get_load_real(&cpu, c->core))
goto end;

cpu_diff.u = cpu.u - c->cpu_prev.u;
Expand All @@ -117,7 +133,14 @@ cpu_get_load(cpu_priv *c)

end:
DBG("total=%f a=%f b=%f\n", total[0], a, b);
g_snprintf(buf, sizeof(buf), "<b>Cpu:</b> %d%%", (int)(total[0] * 100));
if (c->core == 0)
{
g_snprintf(buf, sizeof(buf), "<b>Cpu total:</b> %d%%", (int)(total[0] * 100));
}
else
{
g_snprintf(buf, sizeof(buf), "<b>Cpu's core %u:</b> %d%%", c->core,(int)(total[0] * 100));
}
gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, buf);
k->add_tick(&c->chart, total);
RET(TRUE);
Expand All @@ -136,9 +159,11 @@ cpu_constructor(plugin_instance *p)
c = (cpu_priv *) p;
c->colors[0] = "green";
XCG(p->xc, "Color", &c->colors[0], str);

XCG(p->xc, "Core", (int*) &c->core, int);
if (c->core > sysconf(_SC_NPROCESSORS_CONF))
c->core = sysconf(_SC_NPROCESSORS_CONF);
k->set_rows(&c->chart, 1, c->colors);
gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, "<b>Cpu</b>");
//gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, "<b>Cpu</b>");
cpu_get_load(c);
c->timer = g_timeout_add(1000, (GSourceFunc) cpu_get_load, (gpointer) c);
RET(1);
Expand Down
20 changes: 19 additions & 1 deletion plugins/genmon/genmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ typedef struct {
int time;
int timer;
int max_text_len;
unsigned int max_tooltip_line_len;
unsigned int max_tooltip_lines_count;
char *command;
char *textsize;
char *textcolor;
char* tooltip;
GtkWidget *main;
} genmon_priv;

Expand All @@ -42,8 +45,12 @@ text_update(genmon_priv *gm)
{
FILE *fp;
char text[256];
char tooltip_line[gm->max_tooltip_line_len];
char tooltip_text[gm->max_tooltip_line_len*gm->max_tooltip_lines_count];
char *markup;
tooltip_text[0] = 0;
int len;
int count = 0;

ENTER;
fp = popen(gm->command, "r");
Expand All @@ -59,6 +66,12 @@ text_update(genmon_priv *gm)
gtk_label_set_markup (GTK_LABEL(gm->main), markup);
g_free(markup);
}
fp = popen(gm->tooltip, "r");
while((fgets(tooltip_line,gm->max_tooltip_line_len, fp) != NULL) && (count++ <= gm->max_tooltip_lines_count))
strcat(tooltip_text,tooltip_line);
pclose(fp);
tooltip_text[strlen(tooltip_text)-1] = '\0';
gtk_widget_set_tooltip_markup(gm->plugin.pwid, tooltip_text);
RET(TRUE);
}

Expand Down Expand Up @@ -86,12 +99,17 @@ genmon_constructor(plugin_instance *p)
gm->textsize = "medium";
gm->textcolor = "darkblue";
gm->max_text_len = 30;

gm->max_tooltip_line_len = 100;
gm->max_tooltip_lines_count = 50;

XCG(p->xc, "Command", &gm->command, str);
XCG(p->xc, "TextSize", &gm->textsize, str);
XCG(p->xc, "TextColor", &gm->textcolor, str);
XCG(p->xc, "PollingTime", &gm->time, int);
XCG(p->xc, "MaxTextLength", &gm->max_text_len, int);
XCG(p->xc, "ToolTip", &gm->tooltip, str);
XCG(p->xc, "ToolTipLineLength", (int*) &gm->max_tooltip_line_len, int);
XCG(p->xc, "ToolTipLinesCount", (int*) &gm->max_tooltip_lines_count, int);

gm->main = gtk_label_new(NULL);
gtk_label_set_max_width_chars(GTK_LABEL(gm->main), gm->max_text_len);
Expand Down