Last active
April 24, 2016 02:02
-
-
Save linville/7405013 to your computer and use it in GitHub Desktop.
Simple program to grab statistics from OpenBSD's PF using C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A simple program to show how to grab statistics from OpenBSD's pf using C. | |
gcc pf_to_web.c | |
sudo ./pf_to_web | |
Aaron Linville | |
http://www.linville.org/ | |
*/ | |
#include <sys/types.h> | |
#include <sys/ioctl.h> | |
#include <sys/socket.h> | |
#include <net/if.h> | |
#include <netinet/in.h> | |
#include <net/pfvar.h> | |
#include <err.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) { | |
const char *pf_device = "/dev/pf"; | |
int dev = -1; | |
struct pf_status status; | |
char statline[80], *running; | |
time_t runtime; | |
dev = open(pf_device, O_RDONLY); | |
if (dev == -1) { | |
err(1, "%s", pf_device); | |
} | |
if (ioctl(dev, DIOCGETSTATUS, &status)) { | |
warn("DIOCGETSTATUS"); | |
return (-1); | |
} | |
runtime = time(NULL) - status.since; | |
running = const_cast<char *> (status.running ? "Enabled" : "Disabled"); | |
if (status.since) { | |
unsigned sec, min, hrs, day = runtime; | |
sec = day % 60; | |
day /= 60; | |
min = day % 60; | |
day /= 60; | |
hrs = day % 24; | |
day /= 24; | |
snprintf(statline, sizeof(statline), | |
"Status: %s for %u days %.2u:%.2u:%.2u", | |
running, day, hrs, min, sec); | |
} else { | |
snprintf(statline, sizeof(statline), | |
"Status: %s", running); | |
} | |
printf("<p>%s</p>\n", statline); | |
if (status.ifname[0] != 0) { | |
printf("<p>In: %llu MB</p>\n", | |
(unsigned long long)status.bcounters[0][0] / 1024 / 1024); | |
printf("<p>Out: %llu MB</p>\n", | |
(unsigned long long)status.bcounters[0][1] / 1024 / 1024); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment