#############################################################################*/
#include <errno.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
return NULL;
}
+int collecty_file_read_uint64(collecty_file* self, uint64_t* number) {
+ int r;
+
+ // Read the number
+ r = fscanf(self->f, "%lu", number);
+
+ // Handle errors
+ if (r < 0)
+ return -errno;
+
+ // Exactly one field must have been parsed
+ if (r < 1)
+ return -EBADMSG;
+
+ return 0;
+}
+
int collecty_file_walk(collecty_file* self,
collecty_file_walk_callback callback, void* data) {
unsigned long lineno = 0;
return r;
}
+
+int collecty_read_uint64(collecty_ctx* ctx, const char* path, uint64_t* number) {
+ collecty_file* file = NULL;
+ int r;
+
+ // Open the file
+ r = collecty_file_open_path(&file, ctx, path);
+ if (r < 0)
+ goto ERROR;
+
+ // Read the number
+ r = collecty_file_read_uint64(file, number);
+ if (r < 0)
+ goto ERROR;
+
+ERROR:
+ if (file)
+ collecty_file_unref(file);
+
+ return r;
+}
#ifndef COLLECTY_FILE_H
#define COLLECTY_FILE_H
+#include <stdint.h>
+
typedef struct collecty_file collecty_file;
#include "ctx.h"
collecty_file* collecty_file_ref(collecty_file* self);
collecty_file* collecty_file_unref(collecty_file* self);
+int collecty_file_read_uint64(collecty_file* self, uint64_t* number);
+
typedef int (*collecty_file_walk_callback)
(collecty_ctx* ctx, unsigned long lineno, char* line, size_t length, void* data);
int collecty_file_walk(collecty_file* self,
collecty_file_walk_callback callback, void* data);
+// Shorthands
+
+int collecty_read_uint64(collecty_ctx* ctx, const char* path, uint64_t* number);
+
#endif /* COLLECTY_FILE_H */
#include <string.h>
#include "../ctx.h"
+#include "../file.h"
#include "../source.h"
-#include "../util.h"
#include "conntrack.h"
static int conntrack_heartbeat(collecty_ctx* ctx, collecty_source* source) {
int r;
// Read the total number of connections
- r = collecty_file_read_uint64("/proc/sys/net/netfilter/nf_conntrack_count", &count);
+ r = collecty_read_uint64(ctx, "/proc/sys/net/netfilter/nf_conntrack_count", &count);
if (r < 0) {
ERROR(ctx, "Failed to read %s: %s\n",
"/proc/sys/net/netfilter/nf_conntrack_count", strerror(-r));
}
// Read the maximum number of connections
- r = collecty_file_read_uint64("/proc/sys/net/netfilter/nf_conntrack_max", &max);
+ r = collecty_read_uint64(ctx, "/proc/sys/net/netfilter/nf_conntrack_max", &max);
if (r < 0) {
ERROR(ctx, "Failed to read %s: %s\n",
"/proc/sys/net/netfilter/nf_conntrack_max", strerror(-r));
return __collecty_string_set(buffer, length, "U");
}
-int collecty_file_read_uint64(const char* path, uint64_t* number) {
- unsigned long n = 0;
- FILE* f = NULL;
- int r;
-
- // Open the file
- f = fopen(path, "r");
- if (!f)
- return -errno;
-
- // Read the number
- r = fscanf(f, "%lu", &n);
- if (r != 1) {
- r = -errno;
- goto ERROR;
- }
-
- // Return the number
- *number = n;
- r = 0;
-
-ERROR:
- if (f)
- fclose(f);
-
- return r;
-}
-
// Helper function to set the title
int collecty_format_title(char** title, const char* format, ...) {
va_list args;
int __collecty_format_number(char* buffer, size_t length, int number);
-int collecty_file_read_uint64(const char* path, uint64_t* number);
-
// Helper function to set the title
int collecty_format_title(char** title, const char* format, ...)
__attribute__((format(printf, 2, 3)));