| | 50 | #define NODE_CONTENT(parent, k, v) do { \ |
|---|
| | 51 | xmlNodePtr tmp; \ |
|---|
| | 52 | if(v) { \ |
|---|
| | 53 | tmp = xmlNewNode(NULL, (xmlChar *)(k)); \ |
|---|
| | 54 | xmlNodeAddContent(tmp, (xmlChar *)(v)); \ |
|---|
| | 55 | xmlAddChild(parent, tmp); \ |
|---|
| | 56 | } \ |
|---|
| | 57 | } while(0) |
|---|
| | 58 | |
|---|
| | 59 | xmlNodePtr |
|---|
| | 60 | noit_check_state_as_xml(noit_check_t *check) { |
|---|
| | 61 | xmlNodePtr state, tmp, metrics; |
|---|
| | 62 | noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| | 63 | const char *k; |
|---|
| | 64 | int klen; |
|---|
| | 65 | void *data; |
|---|
| | 66 | stats_t *c = &check->stats.current; |
|---|
| | 67 | |
|---|
| | 68 | state = xmlNewNode(NULL, (xmlChar *)"state"); |
|---|
| | 69 | NODE_CONTENT(state, "running", NOIT_CHECK_RUNNING(check)?"true":"false"); |
|---|
| | 70 | NODE_CONTENT(state, "killed", NOIT_CHECK_KILLED(check)?"true":"false"); |
|---|
| | 71 | NODE_CONTENT(state, "configured", |
|---|
| | 72 | NOIT_CHECK_CONFIGURED(check)?"true":"false"); |
|---|
| | 73 | NODE_CONTENT(state, "disabled", NOIT_CHECK_DISABLED(check)?"true":"false"); |
|---|
| | 74 | xmlAddChild(state, (tmp = xmlNewNode(NULL, (xmlChar *)"last_run"))); |
|---|
| | 75 | if(check->stats.current.whence.tv_sec) { |
|---|
| | 76 | struct timeval f = check->stats.current.whence; |
|---|
| | 77 | struct timeval n; |
|---|
| | 78 | char timestr[20]; |
|---|
| | 79 | gettimeofday(&n, NULL); |
|---|
| | 80 | snprintf(timestr, sizeof(timestr), "%0.3f", |
|---|
| | 81 | n.tv_sec + (n.tv_usec / 1000000.0)); |
|---|
| | 82 | xmlSetProp(tmp, (xmlChar *)"now", (xmlChar *)timestr); |
|---|
| | 83 | snprintf(timestr, sizeof(timestr), "%0.3f", |
|---|
| | 84 | f.tv_sec + (f.tv_usec / 1000000.0)); |
|---|
| | 85 | xmlNodeAddContent(tmp, (xmlChar *)timestr); |
|---|
| | 86 | } |
|---|
| | 87 | if(c->available) { /* truth here means the check has been run */ |
|---|
| | 88 | char buff[20]; |
|---|
| | 89 | snprintf(buff, sizeof(buff), "%0.3f", (float)c->duration/1000.0); |
|---|
| | 90 | NODE_CONTENT(state, "runtime", buff); |
|---|
| | 91 | } |
|---|
| | 92 | NODE_CONTENT(state, "availability", |
|---|
| | 93 | noit_check_available_string(c->available)); |
|---|
| | 94 | NODE_CONTENT(state, "state", noit_check_state_string(c->state)); |
|---|
| | 95 | NODE_CONTENT(state, "status", c->status ? c->status : ""); |
|---|
| | 96 | memset(&iter, 0, sizeof(iter)); |
|---|
| | 97 | xmlAddChild(state, (metrics = xmlNewNode(NULL, (xmlChar *)"metrics"))); |
|---|
| | 98 | while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { |
|---|
| | 99 | char buff[256]; |
|---|
| | 100 | metric_t *m = (metric_t *)data; |
|---|
| | 101 | xmlAddChild(metrics, (tmp = xmlNewNode(NULL, (xmlChar *)m->metric_name))); |
|---|
| | 102 | buff[0] = m->metric_type; buff[1] = '\0'; |
|---|
| | 103 | xmlSetProp(tmp, (xmlChar *)"type", (xmlChar *)buff); |
|---|
| | 104 | if(m->metric_value.s) { |
|---|
| | 105 | int rv; |
|---|
| | 106 | rv = noit_stats_snprint_metric_value(buff, sizeof(buff), m); |
|---|
| | 107 | if(rv < 0) |
|---|
| | 108 | xmlSetProp(tmp, (xmlChar *)"error", (xmlChar *)"unknown type"); |
|---|
| | 109 | else |
|---|
| | 110 | xmlNodeAddContent(tmp, (xmlChar *)buff); |
|---|
| | 111 | } |
|---|
| | 112 | } |
|---|
| | 113 | return state; |
|---|
| | 114 | } |
|---|
| | 115 | |
|---|
| 158 | | else { |
|---|
| 159 | | stats_t *c = &check->stats.current; |
|---|
| 160 | | NODE_CONTENT(state, "running", NOIT_CHECK_RUNNING(check)?"true":"false"); |
|---|
| 161 | | NODE_CONTENT(state, "killed", NOIT_CHECK_KILLED(check)?"true":"false"); |
|---|
| 162 | | NODE_CONTENT(state, "configured", |
|---|
| 163 | | NOIT_CHECK_CONFIGURED(check)?"true":"false"); |
|---|
| 164 | | NODE_CONTENT(state, "disabled", NOIT_CHECK_DISABLED(check)?"true":"false"); |
|---|
| 165 | | xmlAddChild(state, (tmp = xmlNewNode(NULL, (xmlChar *)"last_run"))); |
|---|
| 166 | | if(check->stats.current.whence.tv_sec) { |
|---|
| 167 | | struct timeval f = check->stats.current.whence; |
|---|
| 168 | | struct timeval n; |
|---|
| 169 | | char timestr[20]; |
|---|
| 170 | | gettimeofday(&n, NULL); |
|---|
| 171 | | snprintf(timestr, sizeof(timestr), "%0.3f", |
|---|
| 172 | | n.tv_sec + (n.tv_usec / 1000000.0)); |
|---|
| 173 | | xmlSetProp(tmp, (xmlChar *)"now", (xmlChar *)timestr); |
|---|
| 174 | | snprintf(timestr, sizeof(timestr), "%0.3f", |
|---|
| 175 | | f.tv_sec + (f.tv_usec / 1000000.0)); |
|---|
| 176 | | xmlNodeAddContent(tmp, (xmlChar *)timestr); |
|---|
| 177 | | } |
|---|
| 178 | | if(c->available) { /* truth here means the check has been run */ |
|---|
| 179 | | char buff[20]; |
|---|
| 180 | | snprintf(buff, sizeof(buff), "%0.3f", (float)c->duration/1000.0); |
|---|
| 181 | | NODE_CONTENT(state, "runtime", buff); |
|---|
| 182 | | } |
|---|
| 183 | | NODE_CONTENT(state, "availability", |
|---|
| 184 | | noit_check_available_string(c->available)); |
|---|
| 185 | | NODE_CONTENT(state, "state", noit_check_state_string(c->state)); |
|---|
| 186 | | NODE_CONTENT(state, "status", c->status ? c->status : ""); |
|---|
| 187 | | memset(&iter, 0, sizeof(iter)); |
|---|
| 188 | | xmlAddChild(state, (metrics = xmlNewNode(NULL, (xmlChar *)"metrics"))); |
|---|
| 189 | | while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { |
|---|
| 190 | | char buff[256]; |
|---|
| 191 | | metric_t *m = (metric_t *)data; |
|---|
| 192 | | xmlAddChild(metrics, (tmp = xmlNewNode(NULL, (xmlChar *)m->metric_name))); |
|---|
| 193 | | buff[0] = m->metric_type; buff[1] = '\0'; |
|---|
| 194 | | xmlSetProp(tmp, (xmlChar *)"type", (xmlChar *)buff); |
|---|
| 195 | | if(m->metric_value.s) { |
|---|
| 196 | | int rv; |
|---|
| 197 | | rv = noit_stats_snprint_metric_value(buff, sizeof(buff), m); |
|---|
| 198 | | if(rv < 0) |
|---|
| 199 | | xmlSetProp(tmp, (xmlChar *)"error", (xmlChar *)"unknown type"); |
|---|
| 200 | | else |
|---|
| 201 | | xmlNodeAddContent(tmp, (xmlChar *)buff); |
|---|
| 202 | | } |
|---|
| 203 | | } |
|---|
| 204 | | |
|---|
| 205 | | } |
|---|
| | 216 | } |
|---|
| | 217 | else |
|---|
| | 218 | state = noit_check_state_as_xml(check); |
|---|
| | 219 | xmlAddChild(root, state); |
|---|