C Getting system status from C (or maybe Python)

I need to be able to query a FreeBSD system to find out information about it. Things that I am looking for are CPU utilisation, RAM utilisation, IO utilisation, network utilisation and any other variables that might affect the operation of the FreeBSD system. For instance I might want to query the system for CPU usage to decide whether to deploy another project to it or whether I should spin up a new FreeBSD system programmatically to handle the new project. I also need to be able to create graphs over a period of time to display on a website showing resource usage for each FreeBSD server. It would be even better if I could get information about resource usage for each specific FreeBSD jail running on the system as well. For instance how much CPU is jail ID 1 using?

I'm not sure what the best API is for accessing this data though.
 
You can use sysctl(3) to read various MIBs:
  • CPU: kern.cp_time, filling u_long[5];
  • RAM is at various vm.stats.vm MIBs, like v_active_count, v_free_count, etc.; try sysctl vm.stats.vm (and sysctl -d vm.stats.vm);
  • to read I/O utilisation best use libgeom(3) (see gstat(8) source code);
  • for network utilisation you can read CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, interface_index, IFDATA_GENERAL MIB to fill struct ifmibdata.
You might consider tracking average load, as well, just scale it against number of (physical) processor cores. Basically everything from sysctl -a | less you can read via sysctl(3) (much more elegant, compared to Linux's /proc parsing, IMHO). However some MIB entries will not be listed by sysctl(8). Best read the source code of respective FreeBSD utility.

Having said that, why don't you search for a ready-made utility? Lots of free and OSS monitoring systems out there able to create fancy charts. A handful of existing tools will probably fill in your needs.
 
You can use sysctl(3) to read various MIBs:
  • CPU: kern.cp_time, filling u_long[5];
  • RAM is at various vm.stats.vm MIBs, like v_active_count, v_free_count, etc.; try sysctl vm.stats.vm (and sysctl -d vm.stats.vm);
  • to read I/O utilisation best use libgeom(3) (see gstat(8) source code);
  • for network utilisation you can read CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, interface_index, IFDATA_GENERAL MIB to fill struct ifmibdata.
You might consider tracking average load, as well, just scale it against number of (physical) processor cores. Basically everything from sysctl -a | less you can read via sysctl(3) (much more elegant, compared to Linux's /proc parsing, IMHO). However some MIB entries will not be listed by sysctl(8). Best read the source code of respective FreeBSD utility.

Having said that, why don't you search for a ready-made utility? Lots of free and OSS monitoring systems out there able to create fancy charts. A handful of existing tools will probably fill in your needs.

Awesome. Thank you for the incredibly helpful post.

As to why I don't use a ready-made tool. I'm not sure that they do what I want. It needs to be extremely lightweight as I'm running FreeBSD on memory constrained VPS instances plus I need to be able to add new instances to the cluster entirely programmatically and have the monitoring daemon report to the command and control node almost as soon as the VPS is booted. Plus I also need to support Linux as I might have a few nodes using that. Oh, and I want to be able to measure the resource usage of individual jails on each FreeBSD node since each node will be running multiple jails.

I will have another look at the available options though. It is quite likely there is something out there that I don't know about.
 
Back
Top