本文整理汇总了C++中STATIC_ARRAY_SIZE函数的典型用法代码示例。如果您正苦于以下问题:C++ STATIC_ARRAY_SIZE函数的具体用法?C++ STATIC_ARRAY_SIZE怎么用?C++ STATIC_ARRAY_SIZE使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了STATIC_ARRAY_SIZE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: read_cpuacct_procs
/*
* This callback reads the user/system CPU time for each cgroup.
*/
static int read_cpuacct_procs(const char *dirname, char const *cgroup_name,
void *user_data) {
char abs_path[PATH_MAX];
struct stat statbuf;
char buf[1024];
int status;
FILE *fh;
if (ignorelist_match(il_cgroup, cgroup_name))
return (0);
ssnprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, cgroup_name);
status = lstat(abs_path, &statbuf);
if (status != 0) {
ERROR("cgroups plugin: stat (\"%s\") failed.", abs_path);
return (-1);
}
/* We are only interested in directories, so skip everything else. */
if (!S_ISDIR(statbuf.st_mode))
return (0);
ssnprintf(abs_path, sizeof(abs_path), "%s/%s/cpuacct.stat", dirname,
cgroup_name);
fh = fopen(abs_path, "r");
if (fh == NULL) {
char errbuf[1024];
ERROR("cgroups plugin: fopen (\"%s\") failed: %s", abs_path,
sstrerror(errno, errbuf, sizeof(errbuf)));
return (-1);
}
while (fgets(buf, sizeof(buf), fh) != NULL) {
char *fields[8];
int numfields = 0;
char *key;
size_t key_len;
value_t value;
/* Expected format:
*
* user: 12345
* system: 23456
*
* Or:
*
* user 12345
* system 23456
*/
strstripnewline(buf);
numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
if (numfields != 2)
continue;
key = fields[0];
key_len = strlen(key);
if (key_len < 2)
continue;
/* Strip colon off the first column, if found */
if (key[key_len - 1] == ':')
key[key_len - 1] = 0;
status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
if (status != 0)
continue;
cgroups_submit_one(cgroup_name, key, value);
}
fclose(fh);
return (0);
} /* int read_cpuacct_procs */
示例2: read_pmu
static int read_pmu (void) /* {{{ */
{
int i;
/* The upper limit here is just a safeguard. If there is a system with
* more than 100 batteries, this can easily be increased. */
for (i = 0; i < 100; i++)
{
FILE *fh;
char buffer[1024];
char filename[PATH_MAX];
char plugin_instance[DATA_MAX_NAME_LEN];
gauge_t current = NAN;
gauge_t voltage = NAN;
gauge_t charge = NAN;
ssnprintf (filename, sizeof (filename), PROC_PMU_PATH_FORMAT, i);
if (access (filename, R_OK) != 0)
break;
ssnprintf (plugin_instance, sizeof (plugin_instance), "%i", i);
fh = fopen (filename, "r");
if (fh == NULL)
{
if (errno == ENOENT)
break;
else if ((errno == EAGAIN) || (errno == EINTR))
continue;
else
return (errno);
}
while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
char *fields[8];
int numfields;
numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
if (numfields < 3)
continue;
if (strcmp ("current", fields[0]) == 0)
strtogauge (fields[2], ¤t);
else if (strcmp ("voltage", fields[0]) == 0)
strtogauge (fields[2], &voltage);
else if (strcmp ("charge", fields[0]) == 0)
strtogauge (fields[2], &charge);
}
fclose (fh);
fh = NULL;
battery_submit (plugin_instance, "charge", charge / 1000.0);
battery_submit (plugin_instance, "current", current / 1000.0);
battery_submit (plugin_instance, "voltage", voltage / 1000.0);
}
if (i == 0)
return (ENOENT);
return (0);
} /* }}} int read_pmu */
示例3: c_psql_write
static int c_psql_write (const data_set_t *ds, const value_list_t *vl,
user_data_t *ud)
{
c_psql_database_t *db;
char time_str[32];
char values_name_str[1024];
char values_type_str[1024];
char values_str[1024];
const char *params[9];
int success = 0;
int i;
if ((ud == NULL) || (ud->data == NULL)) {
log_err ("c_psql_write: Invalid user data.");
return -1;
}
db = ud->data;
assert (db->database != NULL);
assert (db->writers != NULL);
if (cdtime_to_iso8601 (time_str, sizeof (time_str), vl->time) == 0) {
log_err ("c_psql_write: Failed to convert time to ISO 8601 format");
return -1;
}
if (values_name_to_sqlarray (ds,
values_name_str, sizeof (values_name_str)) == NULL)
return -1;
#define VALUE_OR_NULL(v) ((((v) == NULL) || (*(v) == '\0')) ? NULL : (v))
params[0] = time_str;
params[1] = vl->host;
params[2] = vl->plugin;
params[3] = VALUE_OR_NULL(vl->plugin_instance);
params[4] = vl->type;
params[5] = VALUE_OR_NULL(vl->type_instance);
params[6] = values_name_str;
#undef VALUE_OR_NULL
pthread_mutex_lock (&db->db_lock);
if (0 != c_psql_check_connection (db)) {
pthread_mutex_unlock (&db->db_lock);
return -1;
}
if ((db->commit_interval > 0)
&& (db->next_commit == 0))
c_psql_begin (db);
for (i = 0; i < db->writers_num; ++i) {
c_psql_writer_t *writer;
PGresult *res;
writer = db->writers[i];
if (values_type_to_sqlarray (ds,
values_type_str, sizeof (values_type_str),
writer->store_rates) == NULL) {
pthread_mutex_unlock (&db->db_lock);
return -1;
}
if (values_to_sqlarray (ds, vl,
values_str, sizeof (values_str),
writer->store_rates) == NULL) {
pthread_mutex_unlock (&db->db_lock);
return -1;
}
params[7] = values_type_str;
params[8] = values_str;
res = PQexecParams (db->conn, writer->statement,
STATIC_ARRAY_SIZE (params), NULL,
(const char *const *)params,
NULL, NULL, /* return text data */ 0);
if ((PGRES_COMMAND_OK != PQresultStatus (res))
&& (PGRES_TUPLES_OK != PQresultStatus (res))) {
PQclear (res);
if ((CONNECTION_OK != PQstatus (db->conn))
&& (0 == c_psql_check_connection (db))) {
/* try again */
res = PQexecParams (db->conn, writer->statement,
STATIC_ARRAY_SIZE (params), NULL,
(const char *const *)params,
NULL, NULL, /* return text data */ 0);
if ((PGRES_COMMAND_OK == PQresultStatus (res))
|| (PGRES_TUPLES_OK == PQresultStatus (res))) {
PQclear (res);
success = 1;
//.........这里部分代码省略.........
示例4: read_file
static int read_file(const char *path) {
FILE *fh;
char key_buffer[4096];
char value_buffer[4096];
char *key_ptr;
char *value_ptr;
char *key_fields[256];
char *value_fields[256];
int key_fields_num;
int value_fields_num;
int status;
int i;
fh = fopen(path, "r");
if (fh == NULL) {
ERROR("protocols plugin: fopen (%s) failed: %s.", path,
sstrerror(errno, key_buffer, sizeof(key_buffer)));
return (-1);
}
status = -1;
while (42) {
clearerr(fh);
key_ptr = fgets(key_buffer, sizeof(key_buffer), fh);
if (key_ptr == NULL) {
if (feof(fh) != 0) {
status = 0;
break;
} else if (ferror(fh) != 0) {
ERROR("protocols plugin: Reading from %s failed.", path);
break;
} else {
ERROR("protocols plugin: fgets failed for an unknown reason.");
break;
}
} /* if (key_ptr == NULL) */
value_ptr = fgets(value_buffer, sizeof(value_buffer), fh);
if (value_ptr == NULL) {
ERROR("protocols plugin: read_file (%s): Could not read values line.",
path);
break;
}
key_ptr = strchr(key_buffer, ':');
if (key_ptr == NULL) {
ERROR("protocols plugin: Could not find protocol name in keys line.");
break;
}
*key_ptr = 0;
key_ptr++;
value_ptr = strchr(value_buffer, ':');
if (value_ptr == NULL) {
ERROR("protocols plugin: Could not find protocol name "
"in values line.");
break;
}
*value_ptr = 0;
value_ptr++;
if (strcmp(key_buffer, value_buffer) != 0) {
ERROR("protocols plugin: Protocol names in keys and values lines "
"don't match: `%s' vs. `%s'.",
key_buffer, value_buffer);
break;
}
key_fields_num =
strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields));
value_fields_num =
strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields));
if (key_fields_num != value_fields_num) {
ERROR("protocols plugin: Number of fields in keys and values lines "
"don't match: %i vs %i.",
key_fields_num, value_fields_num);
break;
}
for (i = 0; i < key_fields_num; i++) {
if (values_list != NULL) {
char match_name[2 * DATA_MAX_NAME_LEN];
ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer,
key_fields[i]);
if (ignorelist_match(values_list, match_name))
continue;
} /* if (values_list != NULL) */
submit(key_buffer, key_fields[i], value_fields[i]);
} /* for (i = 0; i < key_fields_num; i++) */
} /* while (42) */
fclose(fh);
return (status);
} /* int read_file */
示例5: uptime_init
//.........这里部分代码省略.........
}
/* #endif KERNEL_LINUX */
#elif HAVE_LIBKSTAT
kstat_t *ksp;
kstat_named_t *knp;
ksp = NULL;
knp = NULL;
/* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
if (kc == NULL)
{
ERROR ("uptime plugin: kstat chain control structure not available.");
return (-1);
}
ksp = kstat_lookup (kc, "unix", 0, "system_misc");
if (ksp == NULL)
{
ERROR ("uptime plugin: Cannot find unix:0:system_misc kstat.");
return (-1);
}
if (kstat_read (kc, ksp, NULL) < 0)
{
ERROR ("uptime plugin: kstat_read failed.");
return (-1);
}
knp = (kstat_named_t *) kstat_data_lookup (ksp, "boot_time");
if (knp == NULL)
{
ERROR ("uptime plugin: kstat_data_lookup (boot_time) failed.");
return (-1);
}
boottime = (time_t) knp->value.ui32;
if (boottime == 0)
{
ERROR ("uptime plugin: kstat_data_lookup returned success, "
"but `boottime' is zero!");
return (-1);
}
/* #endif HAVE_LIBKSTAT */
# elif HAVE_SYS_SYSCTL_H
struct timeval boottv = { 0 };
size_t boottv_len;
int status;
int mib[] = { CTL_KERN, KERN_BOOTTIME };
boottv_len = sizeof (boottv);
status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &boottv, &boottv_len,
/* new_value = */ NULL, /* new_length = */ 0);
if (status != 0)
{
char errbuf[1024];
ERROR ("uptime plugin: No value read from sysctl interface: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
boottime = boottv.tv_sec;
if (boottime == 0)
{
ERROR ("uptime plugin: sysctl(3) returned success, "
"but `boottime' is zero!");
return (-1);
}
/* #endif HAVE_SYS_SYSCTL_H */
#elif HAVE_PERFSTAT
int status;
perfstat_cpu_total_t cputotal;
int hertz;
status = perfstat_cpu_total(NULL, &cputotal,
sizeof(perfstat_cpu_total_t), 1);
if (status < 0)
{
char errbuf[1024];
ERROR ("uptime plugin: perfstat_cpu_total: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
hertz = sysconf(_SC_CLK_TCK);
if (hertz <= 0)
hertz = HZ;
boottime = time(NULL) - cputotal.lbolt / hertz;
#endif /* HAVE_PERFSTAT */
return (0);
} /* }}} int uptime_init */
示例6: config_cores_parse
int config_cores_parse(const oconfig_item_t *ci, core_groups_list_t *cgl) {
if (ci == NULL || cgl == NULL)
return -EINVAL;
if (ci->values_num == 0 || ci->values_num > MAX_CORES)
return -EINVAL;
core_group_t cgroups[MAX_CORES] = {{0}};
size_t cg_idx = 0; /* index for cgroups array */
int ret = 0;
for (int i = 0; i < ci->values_num; i++) {
if (ci->values[i].type != OCONFIG_TYPE_STRING) {
WARNING(UTIL_NAME ": The %s option requires string arguments.", ci->key);
return -EINVAL;
}
}
if (ci->values_num == 1 && ci->values[0].value.string &&
strlen(ci->values[0].value.string) == 0)
return 0;
for (int i = 0; i < ci->values_num; i++) {
size_t n;
_Bool grouped = 1;
char str[DATA_MAX_NAME_LEN];
unsigned cores[MAX_CORES] = {0};
if (cg_idx >= STATIC_ARRAY_SIZE(cgroups)) {
ERROR(UTIL_NAME
": Configuration exceeds maximum number of cores: %" PRIsz,
STATIC_ARRAY_SIZE(cgroups));
ret = -EINVAL;
goto parse_error;
}
if ((ci->values[i].value.string == NULL) ||
(strlen(ci->values[i].value.string) == 0)) {
ERROR(UTIL_NAME ": Failed to parse parameters for %s option.", ci->key);
ret = -EINVAL;
goto parse_error;
}
ret = check_core_grouping(str, ci->values[i].value.string, sizeof(str),
&grouped);
if (ret != 0) {
ERROR(UTIL_NAME ": Failed to parse config option [%d] %s.", i,
ci->values[i].value.string);
goto parse_error;
}
n = str_list_to_nums(str, cores, STATIC_ARRAY_SIZE(cores));
if (n == 0) {
ERROR(UTIL_NAME ": Failed to parse config option [%d] %s.", i,
ci->values[i].value.string);
ret = -EINVAL;
goto parse_error;
}
if (grouped) {
cgroups[cg_idx].desc = strdup(ci->values[i].value.string);
if (cgroups[cg_idx].desc == NULL) {
ERROR(UTIL_NAME ": Failed to allocate description.");
ret = -ENOMEM;
goto parse_error;
}
cgroups[cg_idx].cores = calloc(n, sizeof(*cgroups[cg_idx].cores));
if (cgroups[cg_idx].cores == NULL) {
ERROR(UTIL_NAME ": Failed to allocate cores for cgroup.");
ret = -ENOMEM;
goto parse_error;
}
for (size_t j = 0; j < n; j++)
cgroups[cg_idx].cores[j] = cores[j];
cgroups[cg_idx].num_cores = n;
cg_idx++;
} else {
for (size_t j = 0; j < n && cg_idx < STATIC_ARRAY_SIZE(cgroups); j++) {
char desc[DATA_MAX_NAME_LEN];
snprintf(desc, sizeof(desc), "%u", cores[j]);
cgroups[cg_idx].desc = strdup(desc);
if (cgroups[cg_idx].desc == NULL) {
ERROR(UTIL_NAME ": Failed to allocate desc for core %u.", cores[j]);
ret = -ENOMEM;
goto parse_error;
}
cgroups[cg_idx].cores = calloc(1, sizeof(*(cgroups[cg_idx].cores)));
if (cgroups[cg_idx].cores == NULL) {
ERROR(UTIL_NAME ": Failed to allocate cgroup for core %u.", cores[j]);
ret = -ENOMEM;
goto parse_error;
}
cgroups[cg_idx].num_cores = 1;
cgroups[cg_idx].cores[0] = cores[j];
cg_idx++;
}
}
}
//.........这里部分代码省略.........
示例7: STATIC_ARRAY_SIZE
#include <pthread.h>
#include <rrd.h>
/*
* Private variables
*/
static int rra_timespans[] =
{
3600,
86400,
604800,
2678400,
31622400
};
static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
static char *rra_types[] =
{
"AVERAGE",
"MIN",
"MAX"
};
static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
#if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
/*
* Private functions
示例8: STATIC_ARRAY_SIZE
{"load_fifteen", "load", "", "longterm", -1, -1},
{"cpu_user", "cpu", "user", "value", -1, -1},
{"cpu_system", "cpu", "system", "value", -1, -1},
{"cpu_idle", "cpu", "idle", "value", -1, -1},
{"cpu_nice", "cpu", "nice", "value", -1, -1},
{"cpu_wio", "cpu", "wait", "value", -1, -1},
{"mem_free", "memory", "free", "value", -1, -1},
{"mem_shared", "memory", "shared", "value", -1, -1},
{"mem_buffers", "memory", "buffered", "value", -1, -1},
{"mem_cached", "memory", "cached", "value", -1, -1},
{"mem_total", "memory", "total", "value", -1, -1},
{"bytes_in", "if_octets", "", "rx", -1, -1},
{"bytes_out", "if_octets", "", "tx", -1, -1},
{"pkts_in", "if_packets", "", "rx", -1, -1},
{"pkts_out", "if_packets", "", "tx", -1, -1}};
static size_t metric_map_len_default = STATIC_ARRAY_SIZE(metric_map_default);
static metric_map_t *metric_map = NULL;
static size_t metric_map_len = 0;
static c_avl_tree_t *staging_tree;
static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER;
static metric_map_t *metric_lookup(const char *key) /* {{{ */
{
metric_map_t *map;
size_t map_len;
size_t i;
/* Search the user-supplied table first.. */
map = metric_map;
示例9: item_watched
static inline int item_watched(int i)
{
assert (i >= 0);
assert (i < ((STATIC_ARRAY_SIZE (watch_items) + 1) * 32));
return watch_items[i / 32] & FLAG (i);
}
示例10: multi4_read
/* for reading status version 4 */
static int multi4_read (const char *name, FILE *fh)
{
char buffer[1024];
char *fields[11];
const int max_fields = STATIC_ARRAY_SIZE (fields);
int fields_num, read = 0;
long long sum_users = 0;
while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
fields_num = openvpn_strsplit (buffer, fields, max_fields);
/* status file is generated by openvpn/multi.c:multi_print_status()
* http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
*
* The line we're expecting has 9 fields. We ignore all lines
* with more or less fields.
*/
if (fields_num != 9)
continue;
if (strcmp (fields[0], "CLIENT_LIST") != 0)
continue;
if (collect_user_count)
/* If so, sum all users, ignore the individuals*/
{
sum_users += 1;
}
if (collect_individual_users)
{
if (new_naming_schema)
{
/* plugin inst = file name, type inst = fields[1] */
iostats_submit (name, /* vpn instance */
fields[1], /* "Common Name" */
atoll (fields[4]), /* "Bytes Received" */
atoll (fields[5])); /* "Bytes Sent" */
}
else
{
/* plugin inst = fields[1], type inst = "" */
iostats_submit (fields[1], /* "Common Name" */
NULL, /* unused when in multimode */
atoll (fields[4]), /* "Bytes Received" */
atoll (fields[5])); /* "Bytes Sent" */
}
}
read = 1;
}
if (collect_user_count)
{
numusers_submit(name, name, sum_users);
read = 1;
}
return (read);
} /* int multi4_read */
示例11: read_acpi_callback
static int read_acpi_callback(char const *dir, /* {{{ */
char const *power_supply, void *user_data) {
int *battery_index = user_data;
gauge_t power = NAN;
gauge_t voltage = NAN;
gauge_t capacity_charged = NAN;
gauge_t capacity_full = NAN;
gauge_t capacity_design = NAN;
_Bool charging = 0;
_Bool is_current = 0;
char const *plugin_instance;
char filename[PATH_MAX];
char buffer[1024];
FILE *fh;
ssnprintf(filename, sizeof(filename), "%s/%s/state", dir, power_supply);
fh = fopen(filename, "r");
if (fh == NULL) {
if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOENT))
return (0);
else
return (errno);
}
/*
* [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
* [11:00] <@tokkee> present: yes
* [11:00] <@tokkee> capacity state: ok
* [11:00] <@tokkee> charging state: charging
* [11:00] <@tokkee> present rate: 1724 mA
* [11:00] <@tokkee> remaining capacity: 4136 mAh
* [11:00] <@tokkee> present voltage: 12428 mV
*/
while (fgets(buffer, sizeof(buffer), fh) != NULL) {
char *fields[8];
int numfields;
numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
if (numfields < 3)
continue;
if ((strcmp(fields[0], "charging") == 0) &&
(strcmp(fields[1], "state:") == 0)) {
if (strcmp(fields[2], "charging") == 0)
charging = 1;
else
charging = 0;
continue;
}
/* The unit of "present rate" depends on the battery. Modern
* batteries export power (watts), older batteries (used to)
* export current (amperes). We check the fourth column and try
* to find old batteries this way. */
if ((strcmp(fields[0], "present") == 0) &&
(strcmp(fields[1], "rate:") == 0)) {
strtogauge(fields[2], &power);
if ((numfields >= 4) && (strcmp("mA", fields[3]) == 0))
is_current = 1;
} else if ((strcmp(fields[0], "remaining") == 0) &&
(strcmp(fields[1], "capacity:") == 0))
strtogauge(fields[2], &capacity_charged);
else if ((strcmp(fields[0], "present") == 0) &&
(strcmp(fields[1], "voltage:") == 0))
strtogauge(fields[2], &voltage);
} /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
fclose(fh);
if (!charging)
power *= -1.0;
/* FIXME: This is a dirty hack for backwards compatibility: The battery
* plugin, for a very long time, has had the plugin_instance
* hard-coded to "0". So, to keep backwards compatibility, we'll use
* "0" for the first battery we find and the power_supply name for all
* following. This should be reverted in a future major version. */
plugin_instance = (*battery_index == 0) ? "0" : power_supply;
(*battery_index)++;
read_acpi_full_capacity(dir, power_supply, &capacity_full, &capacity_design);
submit_capacity(plugin_instance, capacity_charged * PROC_ACPI_FACTOR,
capacity_full * PROC_ACPI_FACTOR,
capacity_design * PROC_ACPI_FACTOR);
battery_submit(plugin_instance, is_current ? "current" : "power",
power * PROC_ACPI_FACTOR);
battery_submit(plugin_instance, "voltage", voltage * PROC_ACPI_FACTOR);
return 0;
} /* }}} int read_acpi_callback */
示例12: multi1_read
/* for reading status version 1 */
static int multi1_read (const char *name, FILE *fh)
{
char buffer[1024];
char *fields[10];
int fields_num, found_header = 0;
long long sum_users = 0;
/* read the file until the "ROUTING TABLE" line is found (no more info after) */
while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
if (strcmp (buffer, "ROUTING TABLE\n") == 0)
break;
if (strcmp (buffer, V1STRING) == 0)
{
found_header = 1;
continue;
}
/* skip the first lines until the client list section is found */
if (found_header == 0)
/* we can't start reading data until this string is found */
continue;
fields_num = openvpn_strsplit (buffer,
fields, STATIC_ARRAY_SIZE (fields));
if (fields_num < 4)
continue;
if (collect_user_count)
/* If so, sum all users, ignore the individuals*/
{
sum_users += 1;
}
if (collect_individual_users)
{
if (new_naming_schema)
{
iostats_submit (name, /* vpn instance */
fields[0], /* "Common Name" */
atoll (fields[2]), /* "Bytes Received" */
atoll (fields[3])); /* "Bytes Sent" */
}
else
{
iostats_submit (fields[0], /* "Common Name" */
NULL, /* unused when in multimode */
atoll (fields[2]), /* "Bytes Received" */
atoll (fields[3])); /* "Bytes Sent" */
}
}
}
if (ferror (fh))
return (0);
if (collect_user_count)
numusers_submit(name, name, sum_users);
return (1);
} /* int multi1_read */
示例13: single_read
static int single_read (const char *name, FILE *fh)
{
char buffer[1024];
char *fields[4];
const int max_fields = STATIC_ARRAY_SIZE (fields);
int fields_num, read = 0;
derive_t link_rx, link_tx;
derive_t tun_rx, tun_tx;
derive_t pre_compress, post_compress;
derive_t pre_decompress, post_decompress;
derive_t overhead_rx, overhead_tx;
link_rx = 0;
link_tx = 0;
tun_rx = 0;
tun_tx = 0;
pre_compress = 0;
post_compress = 0;
pre_decompress = 0;
post_decompress = 0;
while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
fields_num = openvpn_strsplit (buffer, fields, max_fields);
/* status file is generated by openvpn/sig.c:print_status()
* http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
*
* The line we're expecting has 2 fields. We ignore all lines
* with more or less fields.
*/
if (fields_num != 2)
{
continue;
}
if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
{
/* read from the system and sent over the tunnel */
tun_tx = atoll (fields[1]);
}
else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
{
/* read from the tunnel and written in the system */
tun_rx = atoll (fields[1]);
}
else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
{
link_rx = atoll (fields[1]);
}
else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
{
link_tx = atoll (fields[1]);
}
else if (strcmp (fields[0], "pre-compress bytes") == 0)
{
pre_compress = atoll (fields[1]);
}
else if (strcmp (fields[0], "post-compress bytes") == 0)
{
post_compress = atoll (fields[1]);
}
else if (strcmp (fields[0], "pre-decompress bytes") == 0)
{
pre_decompress = atoll (fields[1]);
}
else if (strcmp (fields[0], "post-decompress bytes") == 0)
{
post_decompress = atoll (fields[1]);
}
}
iostats_submit (name, "traffic", link_rx, link_tx);
/* we need to force this order to avoid negative values with these unsigned */
overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
iostats_submit (name, "overhead", overhead_rx, overhead_tx);
if (collect_compression)
{
compression_submit (name, "data_in", post_decompress, pre_decompress);
compression_submit (name, "data_out", pre_compress, post_compress);
}
read = 1;
return (read);
} /* int single_read */
示例14: memory_read_internal
//.........这里部分代码省略.........
gauge_t mem_cached = 0;
gauge_t mem_free = 0;
gauge_t mem_slab_total = 0;
gauge_t mem_slab_reclaimable = 0;
gauge_t mem_slab_unreclaimable = 0;
if ((fh = fopen("/proc/meminfo", "r")) == NULL) {
WARNING("memory: fopen: %s", STRERRNO);
return -1;
}
while (fgets(buffer, sizeof(buffer), fh) != NULL) {
gauge_t *val = NULL;
if (strncasecmp(buffer, "MemTotal:", 9) == 0)
val = &mem_total;
else if (strncasecmp(buffer, "MemFree:", 8) == 0)
val = &mem_free;
else if (strncasecmp(buffer, "Buffers:", 8) == 0)
val = &mem_buffered;
else if (strncasecmp(buffer, "Cached:", 7) == 0)
val = &mem_cached;
else if (strncasecmp(buffer, "Slab:", 5) == 0)
val = &mem_slab_total;
else if (strncasecmp(buffer, "SReclaimable:", 13) == 0) {
val = &mem_slab_reclaimable;
detailed_slab_info = true;
} else if (strncasecmp(buffer, "SUnreclaim:", 11) == 0) {
val = &mem_slab_unreclaimable;
detailed_slab_info = true;
} else
continue;
numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
if (numfields < 2)
continue;
*val = 1024.0 * atof(fields[1]);
}
if (fclose(fh)) {
WARNING("memory: fclose: %s", STRERRNO);
}
if (mem_total < (mem_free + mem_buffered + mem_cached + mem_slab_total))
return -1;
mem_used =
mem_total - (mem_free + mem_buffered + mem_cached + mem_slab_total);
/* SReclaimable and SUnreclaim were introduced in kernel 2.6.19
* They sum up to the value of Slab, which is available on older & newer
* kernels. So SReclaimable/SUnreclaim are submitted if available, and Slab
* if not. */
if (detailed_slab_info)
MEMORY_SUBMIT("used", mem_used, "buffered", mem_buffered, "cached",
mem_cached, "free", mem_free, "slab_unrecl",
mem_slab_unreclaimable, "slab_recl", mem_slab_reclaimable);
else
MEMORY_SUBMIT("used", mem_used, "buffered", mem_buffered, "cached",
mem_cached, "free", mem_free, "slab", mem_slab_total);
/* #endif KERNEL_LINUX */
#elif HAVE_LIBKSTAT
/* Most of the additions here were taken as-is from the k9toolkit from
* Brendan Gregg and are subject to change I guess */
示例15: init
static int init (void)
{
#if PROCESSOR_CPU_LOAD_INFO
kern_return_t status;
port_host = mach_host_self ();
status = host_processors (port_host, &cpu_list, &cpu_list_len);
if (status == KERN_INVALID_ARGUMENT)
{
ERROR ("cpu plugin: Don't have a privileged host control port. "
"The most common cause for this problem is "
"that collectd is running without root "
"privileges, which are required to read CPU "
"load information. "
"<https://collectd.org/bugs/22>");
cpu_list_len = 0;
return (-1);
}
if (status != KERN_SUCCESS)
{
ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status);
cpu_list_len = 0;
return (-1);
}
INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
/* #endif PROCESSOR_CPU_LOAD_INFO */
#elif defined(HAVE_LIBKSTAT)
kstat_t *ksp_chain;
numcpu = 0;
if (kc == NULL)
return (-1);
/* Solaris doesn't count linear.. *sigh* */
for (numcpu = 0, ksp_chain = kc->kc_chain;
(numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
ksp_chain = ksp_chain->ks_next)
if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
ksp[numcpu++] = ksp_chain;
/* #endif HAVE_LIBKSTAT */
#elif CAN_USE_SYSCTL
size_t numcpu_size;
int mib[2] = {CTL_HW, HW_NCPU};
int status;
numcpu = 0;
numcpu_size = sizeof (numcpu);
status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
&numcpu, &numcpu_size, NULL, 0);
if (status == -1)
{
char errbuf[1024];
WARNING ("cpu plugin: sysctl: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
/* #endif CAN_USE_SYSCTL */
#elif defined (HAVE_SYSCTLBYNAME)
size_t numcpu_size;
numcpu_size = sizeof (numcpu);
if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
{
char errbuf[1024];
WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
#ifdef HAVE_SYSCTL_KERN_CP_TIMES
numcpu_size = sizeof (maxcpu);
if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
{
char errbuf[1024];
WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
#else
if (numcpu != 1)
NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
#endif
/* #endif HAVE_SYSCTLBYNAME */
#elif defined(HAVE_LIBSTATGRAB)
/* nothing to initialize */
/* #endif HAVE_LIBSTATGRAB */
#elif defined(HAVE_PERFSTAT)
/* nothing to initialize */
#endif /* HAVE_PERFSTAT */
//.........这里部分代码省略.........