2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

p/multicpu1sec-c: use stdlib IO

This commit is contained in:
Steve Schnepp 2018-04-11 02:47:49 +02:00
parent cc0b094938
commit 52905275b8

View File

@ -95,7 +95,7 @@ int acquire() {
/* fork ourselves if not asked otherwise */ /* fork ourselves if not asked otherwise */
char* no_fork = getenv("no_fork"); char* no_fork = getenv("no_fork");
if (! no_fork || strcmp("1", no_fork)) { if (! no_fork || strcmp("1", no_fork)) {
if (fork()) return; if (fork()) return 0;
// we are the child, complete the daemonization // we are the child, complete the daemonization
/* Close standard IO */ /* Close standard IO */
@ -116,7 +116,12 @@ int acquire() {
int f = open(PROC_STAT, O_RDONLY); int f = open(PROC_STAT, O_RDONLY);
/* open the spoolfile */ /* open the spoolfile */
int cache_file = open(cache_filename, O_CREAT | O_APPEND | O_WRONLY); FILE* cache_file = fopen(cache_filename, "a");
if (!cache_file) {
return fail("cannot create cache_file");
}
int cache_file_fd = fileno(cache_file);
/* loop each second */ /* loop each second */
while (1) { while (1) {
@ -142,7 +147,7 @@ int acquire() {
line = strtok(buffer, newl); line = strtok(buffer, newl);
/* lock */ /* lock */
flock(cache_file, LOCK_EX); flock(cache_file_fd, LOCK_EX);
for (line = strtok(NULL, newl); line; line = strtok(NULL, newl)) { for (line = strtok(NULL, newl); line; line = strtok(NULL, newl)) {
// Not on CPU lines anymore // Not on CPU lines anymore
@ -154,17 +159,15 @@ int acquire() {
long used = usr + nice + sys + iowait + irq + softirq; long used = usr + nice + sys + iowait + irq + softirq;
char out_buffer[1024]; fprintf(cache_file, "%s.value %ld:%ld\n", cpu_id, epoch, used);
sprintf(out_buffer, "%s.value %ld:%ld\n", cpu_id, epoch, used); fflush(cache_file);
write(cache_file, out_buffer, strlen(out_buffer));
} }
/* unlock */ /* unlock */
flock(cache_file, LOCK_UN); flock(cache_file_fd, LOCK_UN);
} }
close(cache_file); fclose(cache_file);
close(f); close(f);
return 0; return 0;
@ -172,9 +175,13 @@ int acquire() {
int fetch() { int fetch() {
FILE* cache_file = fopen(cache_filename, "r+"); FILE* cache_file = fopen(cache_filename, "r+");
if (!cache_file) {
return fail("cannot read cache_file");
}
/* lock */ /* lock */
flock(fileno(cache_file), LOCK_EX); int cache_file_fd = fileno(cache_file);
flock(cache_file_fd, LOCK_EX);
/* cat the cache_file to stdout */ /* cat the cache_file to stdout */
char buffer[1024]; char buffer[1024];
@ -182,7 +189,7 @@ int fetch() {
printf("%s", buffer); printf("%s", buffer);
} }
ftruncate(fileno(cache_file), 0); ftruncate(cache_file_fd, 0);
fclose(cache_file); fclose(cache_file);
return 0; return 0;