diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2020-10-07 18:42:43 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2020-10-07 18:42:43 +0300 |
commit | 335acb7442226c0bbf73974aeeee5c5c92ba3484 (patch) | |
tree | 317dba00e9af3ef372caf53c4622aafacbf19178 | |
parent | 92df83517c284444a2034441bf1367e4a4689e94 (diff) | |
download | tallyman-335acb7442226c0bbf73974aeeee5c5c92ba3484.tar.gz tallyman-335acb7442226c0bbf73974aeeee5c5c92ba3484.tar.bz2 |
Add the container ID of the instance to the InstanceEntry sequence
* src/TALLYMAN-MIB.txt (instanceID): New object.
* src/servdb.c (instance): New member: container_id.
(servdb_update): Set instance.container_id.
(instanceTable_entry_load): Set instanceID.
(instanceTable_entry_free): Free instanceID.
* src/tallyman.c (getcontid): New function.
(main): Set "container_id".
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | src/TALLYMAN-MIB.txt | 21 | ||||
-rw-r--r-- | src/servdb.c | 19 | ||||
-rw-r--r-- | src/tallyman.c | 20 |
4 files changed, 55 insertions, 7 deletions
@@ -34,3 +34,5 @@ depcomp install-sh missing ylwrap +tmp/ +docker/ diff --git a/src/TALLYMAN-MIB.txt b/src/TALLYMAN-MIB.txt index 7f06b73..9a54702 100644 --- a/src/TALLYMAN-MIB.txt +++ b/src/TALLYMAN-MIB.txt @@ -16,12 +16,12 @@ IMPORTS FROM SNMPv2-CONF; tallyman MODULE-IDENTITY - LAST-UPDATED "201806052216Z" + LAST-UPDATED "202010071737Z" ORGANIZATION "Gray Software" CONTACT-INFO "Sergey Poznyakoff <gray@gnu.org>" DESCRIPTION "This MIB module defines objects for Docker service statistics." - REVISION "201806052216Z" + REVISION "202010071737Z" DESCRIPTION "First revision." ::= { enterprises 9163 103 } @@ -126,6 +126,7 @@ InstanceErrorMessage ::= TEXTUAL-CONVENTION InstanceEntry ::= SEQUENCE { instanceIndex Integer32, instanceName InstanceNameString, + instanceID InstanceNameString, instanceService ServiceNameString, instanceState InstanceState, instanceTimeStamp DateAndTime, @@ -165,13 +166,21 @@ instanceName OBJECT-TYPE "The name of the instance." ::= { instanceEntry 2 } +instanceID OBJECT-TYPE + SYNTAX InstanceNameString + MAX-ACCESS read-only + STATUS current + DESCRIPTION + "Container ID of the instance." + ::= { instanceEntry 3 } + instanceService OBJECT-TYPE SYNTAX ServiceNameString MAX-ACCESS read-only STATUS current DESCRIPTION "The name of the instance." - ::= { instanceEntry 3 } + ::= { instanceEntry 4 } instanceState OBJECT-TYPE SYNTAX InstanceState @@ -179,7 +188,7 @@ instanceState OBJECT-TYPE STATUS current DESCRIPTION "State of the instance" - ::= { instanceEntry 4 } + ::= { instanceEntry 5 } instanceTimeStamp OBJECT-TYPE SYNTAX DateAndTime @@ -187,7 +196,7 @@ instanceTimeStamp OBJECT-TYPE STATUS current DESCRIPTION "Time of the last successful probe" - ::= { instanceEntry 5 } + ::= { instanceEntry 6 } instanceErrorMessage OBJECT-TYPE SYNTAX InstanceErrorMessage @@ -196,7 +205,7 @@ instanceErrorMessage OBJECT-TYPE DESCRIPTION "If instanceState is error(3), error message associated with this instance" - ::= { instanceEntry 6 } + ::= { instanceEntry 7 } END diff --git a/src/servdb.c b/src/servdb.c index 3a81bac..dd0f012 100644 --- a/src/servdb.c +++ b/src/servdb.c @@ -22,6 +22,7 @@ enum instance_state { struct instance { char *hostname; + char *container_id; struct service *srv; enum instance_state state; time_t timestamp; @@ -256,6 +257,16 @@ servdb_update(struct json_value *obj) instance_link(inst, srv); } else instance_lock(inst); + + if ((val = json_value_lookup_typed(obj, "container_id", json_string))) { + if (!(inst->container_id && + strcmp(inst->container_id, val->v.s) == 0)) { + free(inst->container_id); + inst->container_id = strdup(val->v.s); + if (!inst->container_id) + grecs_error(NULL, 0, "out of memory"); + } + } time(&inst->timestamp); @@ -445,6 +456,13 @@ instanceTable_entry_load(size_t idx, if (!ent->instanceName) return -1; ent->instanceName_len = strlen(ent->instanceName); + if (inst->container_id) + ent->instanceID = strdup(inst->container_id); + else + ent->instanceID = strdup(""); + if (!ent->instanceID) + return -1; + ent->instanceID_len = strlen(ent->instanceID); ent->instanceService = (char*) service_id; ent->instanceService_len = strlen(service_id); ent->instanceState = inst->state; @@ -528,6 +546,7 @@ instanceTable_entry_free(void *data) { struct instanceTable_entry *ent = data; free(ent->instanceName); + free(ent->instanceID); free(ent->instanceErrorMessage); } diff --git a/src/tallyman.c b/src/tallyman.c index c35cc9e..44cd18a 100644 --- a/src/tallyman.c +++ b/src/tallyman.c @@ -191,11 +191,27 @@ stderr_linemon(const char *bufptr, size_t bufsize, void *closure) fwrite(bufptr, bufsize, 1, stderr); } +char * +getcontid(void) +{ + FILE *fp; + char *id; + + fp = fopen("/proc/self/cgroup", "r"); + if (!fp) + return NULL; + if (fscanf(fp, "%*d:%*[^:]:/%*[^/]/%ms\n", &id) != 1) + id = NULL; + fclose(fp); + return id; +} + int main(int argc, char **argv) { int c, i; char *hostname = NULL; + char *id; char *server = 0; char *value = NULL; char *service_id; @@ -264,7 +280,7 @@ main(int argc, char **argv) exit(1); } } - + parse_server_address(server); if (value) { @@ -278,6 +294,8 @@ main(int argc, char **argv) } json_object_set(obj, "id", json_new_string(service_id)); json_object_set(obj, "hostname", json_new_string(hostname)); + if ((id = getcontid()) != NULL) + json_object_set(obj, "container_id", json_new_string(id)); gettimeofday(&tv, &tz); json_object_set(obj, "timestamp", json_new_number(tv.tv_sec + (double)tv.tv_usec / 1e6)); |