mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-22 02:51:03 +00:00
p/multicpu1sec-c: use posix IO instead of stdlib
This commit is contained in:
parent
7d88587fb6
commit
2db160e268
1 changed files with 16 additions and 9 deletions
|
@ -21,23 +21,30 @@ int fail(char* msg) {
|
|||
|
||||
int config() {
|
||||
/* Get the number of CPU */
|
||||
FILE* f;
|
||||
if ( !(f=fopen(PROC_STAT, "r")) ) {
|
||||
int f;
|
||||
if ( !(f=open(PROC_STAT, O_RDONLY)) ) {
|
||||
return fail("cannot open " PROC_STAT);
|
||||
}
|
||||
|
||||
// Starting with -1, since the first line is the "global cpu line"
|
||||
int ncpu = -1;
|
||||
while (! feof(f)) {
|
||||
char buffer[1024];
|
||||
if (fgets(buffer, 1024, f) == 0) {
|
||||
break;
|
||||
|
||||
const int buffer_size = 64 * 1024;
|
||||
char buffer[buffer_size];
|
||||
|
||||
// whole /proc/stat can be read in 1 syscall
|
||||
if (read(f, buffer, buffer_size) <= 0) {
|
||||
return fail("cannot read " PROC_STAT);
|
||||
}
|
||||
|
||||
if (! strncmp(buffer, "cpu", 3)) ncpu ++;
|
||||
// tokenization per-line
|
||||
char* line;
|
||||
char* newl = "\n";
|
||||
for (line = strtok(buffer, newl); line; line = strtok(NULL, newl)) {
|
||||
if (! strncmp(line, "cpu", 3)) ncpu ++;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
close(f);
|
||||
|
||||
printf(
|
||||
"graph_title multicpu1sec\n"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue