blob: 7270903794339bee407c57c983cd9b070c1f8edb [file] [log] [blame]
Copybara botca5ce642024-11-08 17:38:08 +01001// Licensed under the terms of the GNU GPL v3, or any later version.
2//
3// Copyright 2019 Nolan Leake <nolan@sigbus.net>
4//
5// Loosely based on bandwidth2 (originally by Guillaume Coré <fridim@onfi.re>)
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <time.h>
10#include <string.h>
11#include <unistd.h>
12#include <getopt.h>
13
14#define RED "#FF7373"
15#define ORANGE "#FFA500"
16
17typedef unsigned long long int ulli;
18
19void usage(char *argv[])
20{
21 printf("Usage: %s "
22 "[-t seconds] [-w %%age] [-c %%age] [-d decimals] [-l label] [-h]\n",
23 argv[0]);
24 printf("\n");
25 printf("-t seconds\trefresh time (default is 1)\n");
26 printf("-w %%\tSet warning (color orange) for cpu usage. (default: none)\n");
27 printf("-c %%\tSet critical (color red) for cpu usage. (default: none)\n");
28 printf("-d number\tNumber of decimal places for percentage (default: 2)\n");
29 printf("-l label\tLabel to print before the cpu usage (default: CPU)\n");
30 printf("-h \t\tthis help\n");
31 printf("\n");
32}
33
34void display(const char *label, double used,
35 int const warning, int const critical, int const decimals)
36{
37 if (critical != 0 && used > critical) {
38 printf("%s<span color='%s'>", label, RED);
39 } else if (warning != 0 && used > warning) {
40 printf("%s<span color='%s'>", label, ORANGE);
41 } else {
42 printf("%s<span>", label);
43 }
44
45 printf("%*.*lf%%</span>\n", decimals + 3 + 1, decimals, used);
46}
47
48ulli get_usage(ulli *used_jiffies)
49{
50 FILE *fd = fopen("/proc/stat", "r");
51 ulli user, nice, sys, idle, iowait, irq, sirq, steal, guest, nguest;
52
53 if (!fd) {
54 perror("Couldn't open /proc/stat\n");
55 exit(EXIT_FAILURE);
56 }
57 if (fscanf(fd, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
58 &user, &nice, &sys, &idle, &iowait, &irq, &sirq,
59 &steal, &guest, &nguest) != 10) {
60 perror("Couldn't read jiffies from /proc/stat\n");
61 exit(EXIT_FAILURE);
62 }
63 fclose(fd);
64
65 *used_jiffies = user + nice + sys + irq + sirq + steal + guest + nguest;
66 return *used_jiffies + idle + iowait;
67}
68
69int main(int argc, char *argv[])
70{
71 int warning = 50, critical = 80, t = 1, decimals = 2;
72 char *label = "CPU ";
73 int c;
74 char *envvar = NULL;
75
76 envvar = getenv("REFRESH_TIME");
77 if (envvar)
78 t = atoi(envvar);
79 envvar = getenv("WARN_PERCENT");
80 if (envvar)
81 warning = atoi(envvar);
82 envvar = getenv("CRIT_PERCENT");
83 if (envvar)
84 critical = atoi(envvar);
85 envvar = getenv("DECIMALS");
86 if (envvar)
87 decimals = atoi(envvar);
88 envvar = getenv("LABEL");
89 if (envvar)
90 label = envvar;
91
92 while (c = getopt(argc, argv, "ht:w:c:d:l:"), c != -1) {
93 switch (c) {
94 case 't':
95 t = atoi(optarg);
96 break;
97 case 'w':
98 warning = atoi(optarg);
99 break;
100 case 'c':
101 critical = atoi(optarg);
102 break;
103 case 'd':
104 decimals = atoi(optarg);
105 break;
106 case 'l':
107 label = optarg;
108 break;
109 case 'h':
110 usage(argv);
111 return EXIT_SUCCESS;
112 }
113 }
114
115 ulli old_total;
116 ulli old_used;
117
118 old_total = get_usage(&old_used);
119
120 while (1) {
121 ulli used;
122 ulli total;
123
124 sleep(t);
125 total = get_usage(&used);
126
127 display(label, 100.0D * (used - old_used) / (total - old_total),
128 warning, critical, decimals);
129 fflush(stdout);
130 old_total = total;
131 old_used = used;
132 }
133
134 return EXIT_SUCCESS;
135}