forked from vivien/i3blocks-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_usage2.c
More file actions
135 lines (117 loc) · 3.26 KB
/
Copy pathcpu_usage2.c
File metadata and controls
135 lines (117 loc) · 3.26 KB
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
// Licensed under the terms of the GNU GPL v3, or any later version.
//
// Copyright 2019 Nolan Leake <nolan@sigbus.net>
//
// Loosely based on bandwidth2 (originally by Guillaume Coré <fridim@onfi.re>)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#define RED "#FF7373"
#define ORANGE "#FFA500"
typedef unsigned long long int ulli;
void usage(char *argv[])
{
printf("Usage: %s "
"[-t seconds] [-w %%age] [-c %%age] [-d decimals] [-l label] [-h]\n",
argv[0]);
printf("\n");
printf("-t seconds\trefresh time (default is 1)\n");
printf("-w %%\tSet warning (color orange) for cpu usage. (default: none)\n");
printf("-c %%\tSet critical (color red) for cpu usage. (default: none)\n");
printf("-d number\tNumber of decimal places for percentage (default: 2)\n");
printf("-l label\tLabel to print before the cpu usage (default: CPU)\n");
printf("-h \t\tthis help\n");
printf("\n");
}
void display(const char *label, double used,
int const warning, int const critical, int const decimals)
{
if (critical != 0 && used > critical) {
printf("%s<span color='%s'>", label, RED);
} else if (warning != 0 && used > warning) {
printf("%s<span color='%s'>", label, ORANGE);
} else {
printf("%s<span>", label);
}
printf("%*.*lf%%</span>\n", decimals + 3 + 1, decimals, used);
}
ulli get_usage(ulli *used_jiffies)
{
FILE *fd = fopen("/proc/stat", "r");
ulli user, nice, sys, idle, iowait, irq, sirq, steal, guest, nguest;
if (!fd) {
perror("Couldn't open /proc/stat\n");
exit(EXIT_FAILURE);
}
if (fscanf(fd, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&user, &nice, &sys, &idle, &iowait, &irq, &sirq,
&steal, &guest, &nguest) != 10) {
perror("Couldn't read jiffies from /proc/stat\n");
exit(EXIT_FAILURE);
}
fclose(fd);
*used_jiffies = user + nice + sys + irq + sirq + steal + guest + nguest;
return *used_jiffies + idle + iowait;
}
int main(int argc, char *argv[])
{
int warning = 50, critical = 80, t = 1, decimals = 2;
char *label = "CPU ";
int c;
char *envvar = NULL;
envvar = getenv("REFRESH_TIME");
if (envvar)
t = atoi(envvar);
envvar = getenv("WARN_PERCENT");
if (envvar)
warning = atoi(envvar);
envvar = getenv("CRIT_PERCENT");
if (envvar)
critical = atoi(envvar);
envvar = getenv("DECIMALS");
if (envvar)
decimals = atoi(envvar);
envvar = getenv("LABEL");
if (envvar)
label = envvar;
while (c = getopt(argc, argv, "ht:w:c:d:l:"), c != -1) {
switch (c) {
case 't':
t = atoi(optarg);
break;
case 'w':
warning = atoi(optarg);
break;
case 'c':
critical = atoi(optarg);
break;
case 'd':
decimals = atoi(optarg);
break;
case 'l':
label = optarg;
break;
case 'h':
usage(argv);
return EXIT_SUCCESS;
}
}
ulli old_total;
ulli old_used;
old_total = get_usage(&old_used);
while (1) {
ulli used;
ulli total;
sleep(t);
total = get_usage(&used);
display(label, 100.0D * (used - old_used) / (total - old_total),
warning, critical, decimals);
fflush(stdout);
old_total = total;
old_used = used;
}
return EXIT_SUCCESS;
}