static int mcache_stat_servers(pr_memcache_t *mcache) { memcached_stat_st *mst; memcached_return res; mst = memcached_stat(mcache->mc, NULL, &res); if (mst != NULL) { if (res == MEMCACHED_SUCCESS) { register unsigned int i; const char *stat_keys[] = { "version", "uptime", "curr_connections", "curr_items", "bytes", "limit_maxbytes", NULL }; /* Log some of the stats about the memcached servers to which we just * connected. */ for (i = 0; stat_keys[i] != NULL; i++) { char *info; info = memcached_stat_get_value(mcache->mc, mst, stat_keys[i], &res); if (info != NULL) { pr_trace_msg(trace_channel, 9, "memcached server stats: %s = %s", stat_keys[i], info); free(info); } else { pr_trace_msg(trace_channel, 6, "unable to obtain '%s' stat: %s", stat_keys[i], memcached_strerror(mcache->mc, res)); } } } else { switch (res) { case MEMCACHED_ERRNO: if (errno != EINPROGRESS) { pr_trace_msg(trace_channel, 3, "error requesting memcached stats: system error: %s", strerror(errno)); } else { /* We know that we're not using nonblocking IO; this value usually * means that libmemcached could not connect to the configured * memcached servers. So set the value to something more * indicative, and fall through. */ res = MEMCACHED_CONNECTION_FAILURE; } break; case MEMCACHED_SOME_ERRORS: case MEMCACHED_SERVER_MARKED_DEAD: case MEMCACHED_CONNECTION_FAILURE: { memcached_server_instance_st server; server = memcached_server_get_last_disconnect(mcache->mc); if (server != NULL) { pr_trace_msg(trace_channel, 3, "unable to connect to %s:%d", memcached_server_name(server), memcached_server_port(server)); } break; } default: pr_trace_msg(trace_channel, 6, "error requesting memcached stats: %s", memcached_strerror(mcache->mc, res)); break; } } memcached_stat_free(mcache->mc, mst); } return 0; }
int main(int argc, char ** argv) { struct memcached_st *mc; struct memcached_stat_st *stats; memcached_return_t rc; long cur_conn = -1; int status; char* status_msg; struct timeval tv; long microsec; double elapsed_time; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv = np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); TRACE("%s",">>main"); // initialize gettimeofday(&tv, NULL); mc = memcached_create(NULL); if (mc == NULL) { printf("MEMCACHED %s: failed to memcached_create\n", _("CRITICAL")); exit(EXIT_CRITICAL); } TRACE("[server]%s:%s", mc_host, mc_port); rc = memcached_server_add(mc, mc_host, (in_port_t)mc_port); TRACE("[mc_server_add rv]%d", rc); if (rc != MEMCACHED_SUCCESS) { printf("MEMCACHED %s: failed to memcached_server_add (%d)\n", _("CRITICAL"), rc); exit(EXIT_CRITICAL); } memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1); stats = memcached_stat(mc, NULL, &rc); if (! stats) { printf("MEMCACHED %s: failed to memcached_stats\n", _("CRITICAL")); exit(EXIT_CRITICAL); } cur_conn = stats->curr_connections; memcached_stat_free(mc, stats); memcached_free(mc); status = get_status(cur_conn, my_thresholds); status_msg = _("CRITICAL"); if (status == STATE_OK) { status_msg = _("OK"); } else if (status == STATE_WARNING) { status_msg = _("WARNING"); } else if (status == STATE_CRITICAL) { status_msg = _("CRITICAL"); } microsec = deltime(tv); elapsed_time = (double)microsec / 1.0e6; printf("MEMCACHED %s: conn %ld, %.3f seconds\n", status_msg, cur_conn, elapsed_time); return status; }