Skip to content

Commit

Permalink
Switched from double to u_longlong_t for full precision, since all th…
Browse files Browse the repository at this point in the history
…e counters are in u_longlong_t
  • Loading branch information
thorhs committed Dec 17, 2020
1 parent e4541cd commit 0d5d9dc
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions collectors.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string>
#include <sstream>
#include <functional>
#include <iomanip>

#include <libperfstat.h>
#include <stdio.h>
Expand Down Expand Up @@ -40,21 +41,21 @@ std::string generate_static_labels() {
return output_str;
}

void output_cpus_stat_mode(std::ostringstream& response, const std::string& static_labels, const std::string& name, const std::string& type, const std::string& help, perfstat_cpu_t cpus[], size_t cpu_count, const std::function<double (perfstat_cpu_t&)>& func) {
void output_cpus_stat_mode(std::ostringstream& response, const std::string& static_labels, const std::string& name, const std::string& type, const std::string& help, perfstat_cpu_t cpus[], size_t cpu_count, const std::function<u_longlong_t (perfstat_cpu_t&)>& func) {
response << "# HELP " << name << " " << help << std::endl;
response << "# TYPE " << name << " counter" << std::endl;

for(size_t i=0; i<cpu_count; i++) {
response << name << "{cpu=\"cpu" << i << "\",mode=\"" << type << "\"} " << func(cpus[i]) << std::endl;
response << name << "{cpu=\"cpu" << i << "\",mode=\"" << type << "\"} " << std::fixed << std::setprecision(0) << func(cpus[i]) << std::endl;
}
}

void output_cpus_stat(std::ostringstream& response, const std::string& static_labels, const std::string& name, const std::string& help, perfstat_cpu_t cpus[], size_t cpu_count, const std::function<double (perfstat_cpu_t&)>& func) {
void output_cpus_stat(std::ostringstream& response, const std::string& static_labels, const std::string& name, const std::string& help, perfstat_cpu_t cpus[], size_t cpu_count, const std::function<u_longlong_t (perfstat_cpu_t&)>& func) {
response << "# HELP " << name << " " << help << std::endl;
response << "# TYPE " << name << " counter" << std::endl;

for(size_t i=0; i<cpu_count; i++) {
response << name << "{cpu=\"cpu" << i << "\"," << static_labels << "\"} " << func(cpus[i]) << std::endl;
response << name << "{cpu=\"cpu" << i << "\"," << static_labels << "\"} " << std::fixed << std::setprecision(0) << func(cpus[i]) << std::endl;
}
}

Expand All @@ -71,18 +72,18 @@ void gather_cpus_compat(std::ostringstream& response, const std::string& static_
return;
}

output_cpus_stat_mode(response, static_labels, "node_cpu", "user", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (double)cpu.user/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "idle", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (double)cpu.idle/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "sys", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (double)cpu.sys/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "wait", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (double)cpu.wait/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "user", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (u_longlong_t)cpu.user/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "idle", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (u_longlong_t)cpu.idle/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "sys", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (u_longlong_t)cpu.sys/100; });
output_cpus_stat_mode(response, static_labels, "node_cpu", "wait", "Seconds the cpus spent in each mode.", cpus, cpu_count, [](perfstat_cpu_t& cpu) { return (u_longlong_t)cpu.wait/100; });
}


void output_cpu_stat(std::ostringstream& response, const std::string& static_labels, const std::string& name, const std::string& type, const std::string& help, perfstat_cpu_total_t cpu, const std::function<double (perfstat_cpu_total_t&)>& func) {
void output_cpu_stat(std::ostringstream& response, const std::string& static_labels, const std::string& name, const std::string& type, const std::string& help, perfstat_cpu_total_t cpu, const std::function<u_longlong_t (perfstat_cpu_total_t&)>& func) {
response << "# HELP " << name << " " << help << std::endl;
response << "# TYPE " << name << " " << type << std::endl;

response << name << "{" << static_labels << "} " << func(cpu) << std::endl;
response << name << "{" << static_labels << "} " << std::fixed << std::setprecision(0) << func(cpu) << std::endl;
}

void gather_cpu_compat(std::ostringstream& response, const std::string& static_labels) {
Expand All @@ -93,13 +94,13 @@ void gather_cpu_compat(std::ostringstream& response, const std::string& static_l
return;
}

output_cpu_stat(response, static_labels, "node_load1", "gauge", "1m load average.", cpu, [](perfstat_cpu_total_t& cpu) { return (double)cpu.loadavg[0]/(1<< SBITS); });
output_cpu_stat(response, static_labels, "node_load5", "gauge", "5m load average.", cpu, [](perfstat_cpu_total_t& cpu) { return (double)cpu.loadavg[1]/(1<< SBITS); });
output_cpu_stat(response, static_labels, "node_load15", "gauge", "15m load average.", cpu, [](perfstat_cpu_total_t& cpu) { return (double)cpu.loadavg[2]/(1<< SBITS); });
output_cpu_stat(response, static_labels, "node_load1", "gauge", "1m load average.", cpu, [](perfstat_cpu_total_t& cpu) { return (u_longlong_t)cpu.loadavg[0]/(1<< SBITS); });
output_cpu_stat(response, static_labels, "node_load5", "gauge", "5m load average.", cpu, [](perfstat_cpu_total_t& cpu) { return (u_longlong_t)cpu.loadavg[1]/(1<< SBITS); });
output_cpu_stat(response, static_labels, "node_load15", "gauge", "15m load average.", cpu, [](perfstat_cpu_total_t& cpu) { return (u_longlong_t)cpu.loadavg[2]/(1<< SBITS); });

output_cpu_stat(response, static_labels, "node_context_switches", "counter", "Total number of context switches.", cpu, [](perfstat_cpu_total_t& cpu) { return (double)cpu.pswitch; });
output_cpu_stat(response, static_labels, "node_forks", "counter", "Total number of forks.", cpu, [](perfstat_cpu_total_t& cpu) { return (double)cpu.sysfork; });
output_cpu_stat(response, static_labels, "node_intr", "counter", "Total number of interrupts serviced.", cpu, [](perfstat_cpu_total_t& cpu) { return (double)cpu.decrintrs + (double)cpu.mpcrintrs + (double)cpu.mpcsintrs + (double)cpu.devintrs + (double)cpu.softintrs; });
output_cpu_stat(response, static_labels, "node_context_switches", "counter", "Total number of context switches.", cpu, [](perfstat_cpu_total_t& cpu) { return (u_longlong_t)cpu.pswitch; });
output_cpu_stat(response, static_labels, "node_forks", "counter", "Total number of forks.", cpu, [](perfstat_cpu_total_t& cpu) { return (u_longlong_t)cpu.sysfork; });
output_cpu_stat(response, static_labels, "node_intr", "counter", "Total number of interrupts serviced.", cpu, [](perfstat_cpu_total_t& cpu) { return (u_longlong_t)cpu.decrintrs + (u_longlong_t)cpu.mpcrintrs + (u_longlong_t)cpu.mpcsintrs + (u_longlong_t)cpu.devintrs + (u_longlong_t)cpu.softintrs; });
}

void gather_filesystems(std::ostringstream& response, const std::string& static_labels) {
Expand All @@ -108,37 +109,37 @@ void gather_filesystems(std::ostringstream& response, const std::string& static_
response << "# HELP node_filesystem_size_bytes Filesystem size in bytes." << std::endl;
response << "# TYPE node_filesystem_size_bytes gauge" << std::endl;
for(auto it = filesystems.begin(); it < filesystems.end(); it++) {
response << "node_filesystem_size_bytes{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << (*it).size_bytes << std::endl;
response << "node_filesystem_size_bytes{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << std::fixed << std::setprecision(0) << (*it).size_bytes << std::endl;
}

response << "# HELP node_filesystem_free_bytes Filesystem free space in bytes." << std::endl;
response << "# TYPE node_filesystem_free_bytes gauge" << std::endl;
for(auto it = filesystems.begin(); it < filesystems.end(); it++) {
response << "node_filesystem_free_bytes{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << (*it).free_bytes << std::endl;
response << "node_filesystem_free_bytes{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << std::fixed << std::setprecision(0) << (*it).free_bytes << std::endl;
}

response << "# HELP node_filesystem_avail_bytes Filesystem space available to non-root users in bytes." << std::endl;
response << "# TYPE node_filesystem_avail_bytes gauge" << std::endl;
for(auto it = filesystems.begin(); it < filesystems.end(); it++) {
response << "node_filesystem_avail_bytes{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << (*it).avail_bytes << std::endl;
response << "node_filesystem_avail_bytes{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << std::fixed << std::setprecision(0) << (*it).avail_bytes << std::endl;
}

response << "# HELP node_filesystem_files Filesystem total file nodes." << std::endl;
response << "# TYPE node_filesystem_files gauge" << std::endl;
for(auto it = filesystems.begin(); it < filesystems.end(); it++) {
response << "node_filesystem_files{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << (*it).files << std::endl;
response << "node_filesystem_files{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << std::fixed << std::setprecision(0) << (*it).files << std::endl;
}

response << "# HELP node_filesystem_files_free Filesystem total free file nodes." << std::endl;
response << "# TYPE node_filesystem_files_free gauge" << std::endl;
for(auto it = filesystems.begin(); it < filesystems.end(); it++) {
response << "node_filesystem_files_free{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << (*it).files_free << std::endl;
response << "node_filesystem_files_free{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << std::fixed << std::setprecision(0) << (*it).files_free << std::endl;
}

response << "# HELP node_filesystem_files_avail Filesystem available file nodes to non-root users." << std::endl;
response << "# TYPE node_filesystem_files_avail gauge" << std::endl;
for(auto it = filesystems.begin(); it < filesystems.end(); it++) {
response << "node_filesystem_files_avail{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << (*it).files_avail << std::endl;
response << "node_filesystem_files_avail{device=\"" << (*it).device << "\",fstype=\"jfs2\",mountpoint=\"" << (*it).mountpoint << "\"," << static_labels << "} " << std::fixed << std::setprecision(0) << (*it).files_avail << std::endl;
}
}

Expand Down

0 comments on commit 0d5d9dc

Please sign in to comment.