aboutsummaryrefslogtreecommitdiff
path: root/src/mysqlstat.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-06-03 23:18:56 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2016-06-03 23:18:56 +0300
commitccfe04cf8c3c903013dcacf5681d0d826b222224 (patch)
tree5d26d8d1d19cc13af7ee52048e6f7c6cfe840e31 /src/mysqlstat.c
parentcca955986efc0a7736ea0240fae2ce0694b16c5e (diff)
downloadmysqlstat-ccfe04cf8c3c903013dcacf5681d0d826b222224.tar.gz
mysqlstat-ccfe04cf8c3c903013dcacf5681d0d826b222224.tar.bz2
Initial commit
Diffstat (limited to 'src/mysqlstat.c')
-rw-r--r--src/mysqlstat.c89
1 files changed, 60 insertions, 29 deletions
diff --git a/src/mysqlstat.c b/src/mysqlstat.c
index 1ce7014..c50ae65 100644
--- a/src/mysqlstat.c
+++ b/src/mysqlstat.c
@@ -15,14 +15,40 @@
* along with Mysqlstat. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <mysqlstat.c>
-#include <mysql.c>
+#include <mysqlstat.h>
+#include <mysql/mysql.h>
+#undef NDEBUG
+#include <assert.h>
static char *config_file = CONFDIR "/mysqlstat.cnf";
struct mysqlstat_connection {
MYSQL mysql;
};
+
+static void *
+xalloc(size_t s)
+{
+ void *p = malloc(s);
+ assert(p != NULL);
+ return p;
+}
+
+static void *
+xcalloc(size_t nmemb, size_t size)
+{
+ void *p = calloc(nmemb, size);
+ assert(p != NULL);
+ return p;
+}
+
+static char *
+xstrdup(char const *s)
+{
+ char *p = strdup(s);
+ assert(p != NULL);
+ return p;
+}
mysqlstat_connection_t
mysqlstat_connect(void)
@@ -30,7 +56,7 @@ mysqlstat_connect(void)
struct mysqlstat_connection *conn;
my_bool t = 1;
- conn = xcalloc(sizeof(*conn));
+ conn = xcalloc(1, sizeof(*conn));
mysql_init(&conn->mysql);
if (access(config_file, F_OK) == 0)
mysql_options(&conn->mysql, MYSQL_READ_DEFAULT_FILE,
@@ -48,7 +74,7 @@ mysqlstat_connect(void)
}
void
-mysqlstat_disconnect(mysqlstat_connection_t *conn)
+mysqlstat_disconnect(mysqlstat_connection_t conn)
{
if (!conn)
return;
@@ -85,7 +111,7 @@ syment_free(struct syment *ent)
static int
syment_matches(struct syment *ent, char const *name)
{
- if (!syment->name)
+ if (!ent->name)
return 0;
return strcmp(ent->name, name) == 0;
}
@@ -121,16 +147,15 @@ static void
rehash(struct hashtab *ht)
{
struct syment *oldtab = ht->tab;
- struct syment *newtab;
uint32_t i, n;
assert(ht->hc + 1 < max_rehash);
n = hash_size[ht->hc];
++ht->hc;
- newtab = xcalloc(hash_size[ht->hc], sizeof(newtab[0]));
+ ht->tab = xcalloc(hash_size[ht->hc], sizeof(ht->tab[0]));
for (i = 0; i < n; i++) {
- if (ent->name)
- newtab[find_insert_pos(ht, &oldtab[i])] = oldtab[i];
+ if (oldtab[i].name)
+ ht->tab[find_insert_pos(ht, &oldtab[i])] = oldtab[i];
}
free(oldtab);
}
@@ -138,7 +163,7 @@ rehash(struct hashtab *ht)
static void
hashtab_remove(struct hashtab *ht, char const *name)
{
- uint32_t int pos, i, j, r;
+ uint32_t pos, i, j, r;
pos = hash_string(name, hash_size[ht->hc]);
for (i = pos; ht->tab[i].name;) {
@@ -149,32 +174,32 @@ hashtab_remove(struct hashtab *ht, char const *name)
return;// ENOENT
}
- syment_free(ht->tab[i]);
+ syment_free(&ht->tab[i]);
for (;;) {
- st->tab[i] = NULL;
+ ht->tab[i].name = ht->tab[i].value = NULL;
j = i;
do {
i = (i + 1) % hash_size[ht->hc];
- if (!st->tab[i].name)
+ if (!ht->tab[i].name)
return;
r = hash_string(ht->tab[i].name, hash_size[ht->hc]);
} while ((j < r && r <= i)
|| (i < j && j < r) || (r <= i && i < j));
- st->tab[j] = st->tab[i];
+ ht->tab[j] = ht->tab[i];
}
}
static char const *
hashtab_lookup(struct hashtab *ht, char const *name)
{
- uint32_t pos;
+ uint32_t pos, i;
pos = hash_string(name, hash_size[ht->hc]);
for (i = pos; ht->tab[i].name;) {
if (syment_matches(&ht->tab[i], name))
- return ht->tab[i].name;
+ return ht->tab[i].value;
i = (i + 1) % hash_size[ht->hc];
if (i == pos)
break;
@@ -185,14 +210,17 @@ hashtab_lookup(struct hashtab *ht, char const *name)
static void
hashtab_install(struct hashtab *ht, char const *name, char const *value)
{
- uint32_t pos;
+ uint32_t i, pos;
struct syment *ent;
-
+
+ if (!value)
+ value = "";
+ DEBUGMSGTL(("mysqlstat", "%s = %s\n", name, value));
pos = hash_string(name, hash_size[ht->hc]);
for (i = pos; ht->tab[i].name;) {
if (syment_matches(&ht->tab[i], name)) {
free(ht->tab[i].value);
- ht->tab[i].value = xtsrdup(value);
+ ht->tab[i].value = xstrdup(value);
return;
}
i = (i + 1) % hash_size[ht->hc];
@@ -202,8 +230,8 @@ hashtab_install(struct hashtab *ht, char const *name, char const *value)
}
}
- ht->tab[i].name = xtsrdup(name);
- ht->tab[i].value = xtsrdup(value);
+ ht->tab[i].name = xstrdup(name);
+ ht->tab[i].value = xstrdup(value);
}
void
@@ -211,7 +239,7 @@ hashtab_clear(struct hashtab *ht)
{
unsigned i, hs;
- hs = hash_size[st->hash_num];
+ hs = hash_size[ht->hc];
for (i = 0; i < hs; i++) {
syment_free(&ht->tab[i]);
}
@@ -220,15 +248,15 @@ hashtab_clear(struct hashtab *ht)
static struct hashtab *
hashtab_create(void)
{
- struct hashtab *ht = xmalloc(sizeof(*ht));
+ struct hashtab *ht = xalloc(sizeof(*ht));
ht->hc = 0;
- ht->tab = xcalloc(hash_size[ht->hc], sizeof(newtab[0]));
+ ht->tab = xcalloc(hash_size[ht->hc], sizeof(ht->tab[0]));
return ht;
}
struct mysqlstat_cache {
- void (*populate) (struch hashtab *);
- struch hashtab *ht;
+ void (*populate) (struct hashtab *);
+ struct hashtab *ht;
time_t ts;
};
@@ -251,7 +279,7 @@ mysqlstat_cache_get(struct mysqlstat_cache *cache, char const *name)
}
static void
-populate_repl(struch hashtab *ht)
+populate_repl(struct hashtab *ht)
{
mysqlstat_connection_t conn;
MYSQL_RES *res;
@@ -263,6 +291,7 @@ populate_repl(struch hashtab *ht)
if (!conn)
return;
+ DEBUGMSGTL(("mysqlstat", "Getting slave status\n"));
if (mysql_query(&conn->mysql, "SHOW SLAVE STATUS")) {
snmp_log(LOG_ERR, "can't get slave status: %s",
mysql_error(&conn->mysql));
@@ -279,7 +308,8 @@ populate_repl(struch hashtab *ht)
fields = mysql_fetch_fields(res);
row = mysql_fetch_row(res);
for (i = 0; i < num_fields; i++)
- hashtab_install(ht, fields[i].name, row[i] ? row[i] : NULL);
+ if (row[i])
+ hashtab_install(ht, fields[i].name, row[i]);
mysql_free_result(res);
mysqlstat_disconnect(conn);
}
@@ -291,7 +321,8 @@ static struct mysqlstat_cache cache[MYSQLSTAT_MAX_CACHE] = {
char const *
mysqlstat_get(int id, char const *name)
{
- return mysqlstat_cache_get(cache + i, name);
+ assert(id < sizeof(cache) / sizeof(cache[0]));
+ return mysqlstat_cache_get(cache + id, name);
}

Return to:

Send suggestions and report system problems to the System administrator.