static int mysql_read (user_data_t *ud)
{
mysql_database_t *db;
MYSQL *con;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
derive_t qcache_hits = 0;
derive_t qcache_inserts = 0;
derive_t qcache_not_cached = 0;
derive_t qcache_lowmem_prunes = 0;
gauge_t qcache_queries_in_cache = NAN;
gauge_t threads_running = NAN;
gauge_t threads_connected = NAN;
gauge_t threads_cached = NAN;
derive_t threads_created = 0;
unsigned long long traffic_incoming = 0ULL;
unsigned long long traffic_outgoing = 0ULL;
unsigned long mysql_version = 0ULL;
if ((ud == NULL) || (ud->data == NULL))
{
ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
return (-1);
}
db = (mysql_database_t *) ud->data;
/* An error message will have been printed in this case */
if ((con = getconnection (db)) == NULL)
return (-1);
mysql_version = mysql_get_server_version(con);
query = "SHOW STATUS";
if (mysql_version >= 50002)
query = "SHOW GLOBAL STATUS";
res = exec_query (con, query);
if (res == NULL)
return (-1);
while ((row = mysql_fetch_row (res)))
{
char *key;
unsigned long long val;
key = row[0];
val = atoll (row[1]);
if (strncmp (key, "Com_",
strlen ("Com_")) == 0)
{
if (val == 0ULL)
continue;
/* Ignore `prepared statements' */
if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
counter_submit ("mysql_commands",
key + strlen ("Com_"),
val, db);
}
else if (strncmp (key, "Handler_",
strlen ("Handler_")) == 0)
{
if (val == 0ULL)
continue;
counter_submit ("mysql_handler",
key + strlen ("Handler_"),
val, db);
}
else if (strncmp (key, "Qcache_",
strlen ("Qcache_")) == 0)
{
if (strcmp (key, "Qcache_hits") == 0)
qcache_hits = (derive_t) val;
else if (strcmp (key, "Qcache_inserts") == 0)
qcache_inserts = (derive_t) val;
else if (strcmp (key, "Qcache_not_cached") == 0)
qcache_not_cached = (derive_t) val;
else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
qcache_lowmem_prunes = (derive_t) val;
else if (strcmp (key, "Qcache_queries_in_cache") == 0)
qcache_queries_in_cache = (gauge_t) val;
}
else if (strncmp (key, "Bytes_",
strlen ("Bytes_")) == 0)
{
if (strcmp (key, "Bytes_received") == 0)
traffic_incoming += val;
else if (strcmp (key, "Bytes_sent") == 0)
traffic_outgoing += val;
}
else if (strncmp (key, "Threads_",
strlen ("Threads_")) == 0)
{
//.........这里部分代码省略.........
开发者ID:baloo,项目名称:collectd,代码行数:101,代码来源:mysql.c
示例5: spin_lock
//.........这里部分代码省略.........
if (n == cache->entries) {
/*
* Block not in cache, if all cache entries are used
* go to sleep waiting for one to become available.
*/
if (cache->unused == 0) {
cache->num_waiters++;
spin_unlock(&cache->lock);
wait_event(cache->wait_queue, cache->unused);
spin_lock(&cache->lock);
cache->num_waiters--;
continue;
}
/*
* At least one unused cache entry. A simple
* round-robin strategy is used to choose the entry to
* be evicted from the cache.
*/
i = cache->next_blk;
for (n = 0; n < cache->entries; n++) {
if (cache->entry[i].refcount == 0)
break;
i = (i + 1) % cache->entries;
}
cache->next_blk = (i + 1) % cache->entries;
entry = &cache->entry[i];
/*
* Initialise chosen cache entry, and fill it in from
* disk.
*/
cache->unused--;
entry->block = block;
entry->refcount = 1;
entry->pending = 1;
entry->num_waiters = 0;
entry->error = 0;
spin_unlock(&cache->lock);
entry->length = squashfs_read_data(sb, block, length,
&entry->next_index, entry->actor);
spin_lock(&cache->lock);
if (entry->length < 0)
entry->error = entry->length;
entry->pending = 0;
/*
* While filling this entry one or more other processes
* have looked it up in the cache, and have slept
* waiting for it to become available.
*/
if (entry->num_waiters) {
spin_unlock(&cache->lock);
wake_up_all(&entry->wait_queue);
} else
spin_unlock(&cache->lock);
goto out;
}
/*
* Block already in cache. Increment refcount so it doesn't
* get reused until we're finished with it, if it was
* previously unused there's one less cache entry available
* for reuse.
*/
entry = &cache->entry[i];
if (entry->refcount == 0)
cache->unused--;
entry->refcount++;
/*
* If the entry is currently being filled in by another process
* go to sleep waiting for it to become available.
*/
if (entry->pending) {
entry->num_waiters++;
spin_unlock(&cache->lock);
wait_event(entry->wait_queue, !entry->pending);
} else
spin_unlock(&cache->lock);
goto out;
}
out:
TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
cache->name, i, entry->block, entry->refcount, entry->error);
if (entry->error)
ERROR("Unable to read %s cache entry [%llx]\n", cache->name,
block);
return entry;
}
//.........这里部分代码省略.........
WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
status = -1;
}
if (status != 0)
break;
} /* for (ci->children) */
while (status == 0)
{
if (hd->address == NULL)
{
WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
status = -1;
break;
}
if (hd->community == NULL && hd->version < 3)
{
WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
status = -1;
break;
}
if (hd->version == 3)
{
if (hd->username == NULL)
{
WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
status = -1;
break;
}
if (hd->security_level == 0)
{
WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
status = -1;
break;
}
if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
{
if (hd->auth_protocol == NULL)
{
WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
status = -1;
break;
}
if (hd->auth_passphrase == NULL)
{
WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
status = -1;
break;
}
}
if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
{
if (hd->priv_protocol == NULL)
{
WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
status = -1;
break;
}
if (hd->priv_passphrase == NULL)
{
WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
status = -1;
break;
}
}
}
break;
} /* while (status == 0) */
if (status != 0)
{
csnmp_host_definition_destroy (hd);
return (-1);
}
DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
hd->name, hd->address, hd->community, hd->version);
ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
memset (&cb_data, 0, sizeof (cb_data));
cb_data.data = hd;
cb_data.free_func = csnmp_host_definition_destroy;
CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
status = plugin_register_complex_read (/* group = */ NULL, cb_name,
csnmp_read_host, /* interval = */ &cb_interval,
/* user_data = */ &cb_data);
if (status != 0)
{
ERROR ("snmp plugin: Registering complex read function failed.");
csnmp_host_definition_destroy (hd);
return (-1);
}
return (0);
} /* int csnmp_config_add_host */
开发者ID:BrianB2,项目名称:collectd,代码行数:101,代码来源:snmp.c
示例12: csnmp_read_table
static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
{
struct snmp_pdu *req;
struct snmp_pdu *res;
struct variable_list *vb;
const data_set_t *ds;
uint32_t oid_list_len = (uint32_t) (data->values_len + 1);
/* Holds the last OID returned by the device. We use this in the GETNEXT
* request to proceed. */
oid_t oid_list[oid_list_len];
/* Set to false when an OID has left its subtree so we don't re-request it
* again. */
_Bool oid_list_todo[oid_list_len];
int status;
int i;
uint32_t j;
/* `value_list_head' and `value_list_tail' implement a linked list for each
* value. `instance_list_head' and `instance_list_tail' implement a linked list of
* instance names. This is used to jump gaps in the table. */
csnmp_list_instances_t *instance_list_head;
csnmp_list_instances_t *instance_list_tail;
csnmp_table_values_t **value_list_head;
csnmp_table_values_t **value_list_tail;
DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
host->name, data->name);
if (host->sess_handle == NULL)
{
DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
return (-1);
}
ds = plugin_get_ds (data->type);
if (!ds)
{
ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
return (-1);
}
if (ds->ds_num != data->values_len)
{
ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
data->type, ds->ds_num, data->values_len);
return (-1);
}
/* We need a copy of all the OIDs, because GETNEXT will destroy them. */
memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
if (data->instance.oid.oid_len > 0)
memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
else /* no InstanceFrom option specified. */
oid_list_len--;
for (j = 0; j < oid_list_len; j++)
oid_list_todo[j] = 1;
/* We're going to construct n linked lists, one for each "value".
* value_list_head will contain pointers to the heads of these linked lists,
* value_list_tail will contain pointers to the tail of the lists. */
value_list_head = calloc (data->values_len, sizeof (*value_list_head));
value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
if ((value_list_head == NULL) || (value_list_tail == NULL))
{
ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
sfree (value_list_head);
sfree (value_list_tail);
return (-1);
}
instance_list_head = NULL;
instance_list_tail = NULL;
status = 0;
while (status == 0)
{
int oid_list_todo_num;
req = snmp_pdu_create (SNMP_MSG_GETNEXT);
if (req == NULL)
{
ERROR ("snmp plugin: snmp_pdu_create failed.");
status = -1;
break;
}
oid_list_todo_num = 0;
for (j = 0; j < oid_list_len; j++)
{
/* Do not rerequest already finished OIDs */
if (!oid_list_todo[j])
continue;
oid_list_todo_num++;
snmp_add_null_var (req, oid_list[j].oid, oid_list[j].oid_len);
}
//.........这里部分代码省略.........
开发者ID:BrianB2,项目名称:collectd,代码行数:101,代码来源:snmp.c
示例13: csnmp_dispatch_table
static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
csnmp_list_instances_t *instance_list,
csnmp_table_values_t **value_table)
{
const data_set_t *ds;
value_list_t vl = VALUE_LIST_INIT;
csnmp_list_instances_t *instance_list_ptr;
csnmp_table_values_t **value_table_ptr;
int i;
_Bool have_more;
oid_t current_suffix;
ds = plugin_get_ds (data->type);
if (!ds)
{
ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
return (-1);
}
assert (ds->ds_num == data->values_len);
instance_list_ptr = instance_list;
value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
if (value_table_ptr == NULL)
return (-1);
for (i = 0; i < data->values_len; i++)
value_table_ptr[i] = value_table[i];
vl.values_len = ds->ds_num;
vl.values = malloc (sizeof (*vl.values) * vl.values_len);
if (vl.values == NULL)
{
ERROR ("snmp plugin: malloc failed.");
sfree (value_table_ptr);
return (-1);
}
sstrncpy (vl.host, host->name, sizeof (vl.host));
sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
vl.interval = host->interval;
have_more = 1;
memset (¤t_suffix, 0, sizeof (current_suffix));
while (have_more)
{
_Bool suffix_skipped = 0;
/* Determine next suffix to handle. */
if (instance_list != NULL)
{
if (instance_list_ptr == NULL)
{
have_more = 0;
continue;
}
memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
}
else /* no instance configured */
{
csnmp_table_values_t *ptr = value_table_ptr[0];
if (ptr == NULL)
{
have_more = 0;
continue;
}
memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
}
/* Update all the value_table_ptr to point at the entry with the same
* trailing partial OID */
for (i = 0; i < data->values_len; i++)
{
while ((value_table_ptr[i] != NULL)
&& (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
value_table_ptr[i] = value_table_ptr[i]->next;
if (value_table_ptr[i] == NULL)
{
have_more = 0;
break;
}
else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
{
/* This suffix is missing in the subtree. Indicate this with the
* "suffix_skipped" flag and try the next instance / suffix. */
suffix_skipped = 1;
break;
}
} /* for (i = 0; i < columns; i++) */
if (!have_more)
break;
/* Matching the values failed. Start from the beginning again. */
if (suffix_skipped)
//.........这里部分代码省略.........
开发者ID:BrianB2,项目名称:collectd,代码行数:101,代码来源:snmp.c
示例14: csnmp_instance_list_add
static int csnmp_instance_list_add (csnmp_list_instances_t **head,
csnmp_list_instances_t **tail,
const struct snmp_pdu *res,
const host_definition_t *hd, const data_definition_t *dd)
{
csnmp_list_instances_t *il;
struct variable_list *vb;
oid_t vb_name;
int status;
uint32_t i;
uint32_t is_matched;
/* Set vb on the last variable */
for (vb = res->variables;
(vb != NULL) && (vb->next_variable != NULL);
vb = vb->next_variable)
/* do nothing */;
if (vb == NULL)
return (-1);
csnmp_oid_init (&vb_name, vb->name, vb->name_length);
il = malloc (sizeof (*il));
if (il == NULL)
{
ERROR ("snmp plugin: malloc failed.");
return (-1);
}
memset (il, 0, sizeof (*il));
il->next = NULL;
status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
if (status != 0)
{
sfree (il);
return (status);
}
/* Get instance name */
if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
{
char *ptr;
csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
is_matched = 0;
for (i = 0; i < dd->ignores_len; i++)
{
status = fnmatch(dd->ignores[i], il->instance, 0);
if (status == 0)
{
if (dd->invert_match == 0)
{
sfree(il);
return 0;
}
else
{
is_matched = 1;
break;
}
}
}
if (dd->invert_match != 0 && is_matched == 0)
{
sfree(il);
return 0;
}
for (ptr = il->instance; *ptr != '\0'; ptr++)
{
if ((*ptr > 0) && (*ptr < 32))
*ptr = ' ';
else if (*ptr == '/')
*ptr = '_';
}
DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
}
else
{
value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
/* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
ssnprintf (il->instance, sizeof (il->instance),
"%llu", val.counter);
}
/* TODO: Debugging output */
if (*head == NULL)
*head = il;
else
(*tail)->next = il;
*tail = il;
return (0);
} /* int csnmp_instance_list_add */
开发者ID:BrianB2,项目名称:collectd,代码行数:94,代码来源:snmp.c
示例15: parse_flags
static int parse_flags(char *flags, struct flag_list *fl,
struct fs_mgr_flag_values *flag_vals,
char *fs_options, int fs_options_len)
{
int f = 0;
int i;
char *p;
char *savep;
/* initialize flag values. If we find a relevant flag, we'll
* update the value */
if (flag_vals) {
memset(flag_vals, 0, sizeof(*flag_vals));
flag_vals->partnum = -1;
flag_vals->swap_prio = -1; /* negative means it wasn't specified. */
}
/* initialize fs_options to the null string */
if (fs_options && (fs_options_len > 0)) {
fs_options[0] = '\0';
}
p = strtok_r(flags, ",", &savep);
while (p) {
/* Look for the flag "p" in the flag list "fl"
* If not found, the loop exits with fl[i].name being null.
*/
for (i = 0; fl[i].name; i++) {
if (!strncmp(p, fl[i].name, strlen(fl[i].name))) {
f |= fl[i].flag;
if ((fl[i].flag == MF_CRYPT) && flag_vals) {
/* The encryptable flag is followed by an = and the
* location of the keys. Get it and return it.
*/
flag_vals->key_loc = strdup(strchr(p, '=') + 1);
} else if ((fl[i].flag == MF_VERIFY) && flag_vals) {
/* If the verify flag is followed by an = and the
* location for the verity state, get it and return it.
*/
char *start = strchr(p, '=');
if (start) {
flag_vals->verity_loc = strdup(start + 1);
}
} else if ((fl[i].flag == MF_FORCECRYPT) && flag_vals) {
/* The forceencrypt flag is followed by an = and the
* location of the keys. Get it and return it.
*/
flag_vals->key_loc = strdup(strchr(p, '=') + 1);
} else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
/* The length flag is followed by an = and the
* size of the partition. Get it and return it.
*/
flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0);
} else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) {
/* The voldmanaged flag is followed by an = and the
* label, a colon and the partition number or the
* word "auto", e.g.
* voldmanaged=sdcard:3
* Get and return them.
*/
char *label_start;
char *label_end;
char *part_start;
label_start = strchr(p, '=') + 1;
label_end = strchr(p, ':');
if (label_end) {
flag_vals->label = strndup(label_start,
(int) (label_end - label_start));
part_start = strchr(p, ':') + 1;
if (!strcmp(part_start, "auto")) {
flag_vals->partnum = -1;
} else {
flag_vals->partnum = strtol(part_start, NULL, 0);
}
} else {
ERROR("Warning: voldmanaged= flag malformed\n");
}
} else if ((fl[i].flag == MF_SWAPPRIO) && flag_vals) {
flag_vals->swap_prio = strtoll(strchr(p, '=') + 1, NULL, 0);
} else if ((fl[i].flag == MF_ZRAMSIZE) && flag_vals) {
flag_vals->zram_size = strtoll(strchr(p, '=') + 1, NULL, 0);
}
break;
}
}
if (!fl[i].name) {
if (fs_options) {
/* It's not a known flag, so it must be a filesystem specific
* option. Add it to fs_options if it was passed in.
*/
strlcat(fs_options, p, fs_options_len);
strlcat(fs_options, ",", fs_options_len);
} else {
/* fs_options was not passed in, so if the flag is unknown
* it's an error.
*/
ERROR("Warning: unknown flag %s\n", p);
}
//.........这里部分代码省略.........
请发表评论