Last active
October 21, 2016 11:39
-
-
Save thebsdbox/c8c5ac7bd85d9b48a2888d0e4c300348 to your computer and use it in GitHub Desktop.
OV JSON drove me to drink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void stats(oneviewSession *session) | |
{ | |
// This test function will grab Server-Profiles | |
// It will then locate the bay that their located in | |
// Finally it will locate the sub ports and get the statistics and names | |
// Presume we're logged in before trying to query OneView | |
if ((session) && session->address && session->cookie) { | |
// First query | |
char *profiles = ovQueryServerProfiles(session, NULL); | |
if (profiles) { | |
json_t *profileJSON; | |
json_error_t error; | |
struct interconnectLookup icLookup[2]; | |
size_t icIndex = 0; | |
// Parse the JSON | |
profileJSON = json_loads(profiles, 0, &error); | |
// If the JSON was loaded correctly attempt to parse it | |
if (profileJSON) { | |
json_t *memberArray = json_object_get(profileJSON, "members"); | |
if (memberArray) { | |
json_incref(profileJSON); // Increase reference count due to new usage of reference | |
// Check that the array size is more than zero | |
if (json_array_size(memberArray) != 0) { | |
size_t memberIndex, connectionIndex, portIndex, subPortIndex; | |
json_t *memberValue, *connectionValue, *portValue, *subPortValue; | |
json_array_foreach(memberArray, memberIndex, memberValue) { | |
// retrieve needed statistics | |
const char *name = json_string_value(json_object_get(memberValue, "name")); | |
size_t enclosureBay = json_integer_value(json_object_get(memberValue, "enclosureBay")); | |
printf("Found Server: %s at bay %lu", name, enclosureBay); | |
json_t *connectionArray = json_object_get(memberValue, "connections"); | |
char interconnectPortName[4]; | |
sprintf(interconnectPortName, "d%zu", enclosureBay); | |
if (connectionArray) { | |
json_incref(profileJSON); | |
if (json_array_size(connectionArray) != 0) { | |
json_array_foreach(connectionArray, connectionIndex, connectionValue) { | |
// Get name of connection ethX or hbaX | |
const char *connectionName = json_string_value(json_object_get(connectionValue, "name")); | |
const char *portName = json_string_value(json_object_get(connectionValue, "portId")); | |
printf(" %s", connectionName); | |
// Translate port from -a/b/c to a subPort number (KILL ME) | |
const char *explicitPort = portName+(strlen(portName) - 1); | |
int subPort = 0; | |
if (stringMatch((char *) explicitPort, "a")) { | |
subPort = 1; | |
} else if (stringMatch((char *) explicitPort, "b")) { | |
subPort = 2; | |
} else if (stringMatch((char *) explicitPort, "c")) { | |
subPort = 3; | |
} | |
const char *interconnect = json_string_value(json_object_get(connectionValue, "interconnectUri")); | |
// Once the interconnect cache has both interconnects added along with their associated json we can step over. | |
if (icIndex <2) { | |
icLookup[icIndex].interconnectName = strdup(interconnect); | |
icLookup[icIndex].interconnectJSON = json_loads(ovQueryInterconnectStatisticsWithURI(session, NULL, (char *)interconnect), 0, &error); | |
json_t *port = json_object_get(icLookup[icIndex].interconnectJSON, "portStatistics"); | |
json_array_foreach(port, portIndex, portValue) { | |
if (stringMatch(interconnectPortName, (char *)json_string_value(json_object_get(portValue, "portName")))) { | |
icLookup[icIndex].portStatisticsJSON = json_object_get(portValue, "subportStatistics"); | |
} | |
} | |
icIndex++; | |
} | |
// Determins the internconnect used for the subport and get it's statistics. | |
if (stringMatch( (char *)interconnect, (char *)icLookup[0].interconnectName)) { | |
json_array_foreach(icLookup[0].portStatisticsJSON, subPortIndex, subPortValue) { | |
if (subPort == json_integer_value(json_object_get(subPortValue, "subportNumber"))) { | |
printf("\n %s", json_string_value(json_object_get(json_object_get(subPortValue, "subportAdvancedStatistics"), "receiveKilobitsPerSec"))); | |
} | |
} | |
} else if (stringMatch( (char *)interconnect, (char *)icLookup[1].interconnectName)) { | |
json_array_foreach(icLookup[0].portStatisticsJSON, subPortIndex, subPortValue) { | |
if (subPort == json_integer_value(json_object_get(subPortValue, "subportNumber"))) { | |
printf("\n %s", json_string_value(json_object_get(json_object_get(subPortValue, "subportAdvancedStatistics"), "receiveKilobitsPerSec"))); | |
} | |
} | |
} | |
} | |
printf("\n"); | |
} | |
} | |
// Decrease reference to the Connection Array | |
json_decref(connectionArray); | |
// Decrease the refcount to the profile JSON | |
json_decref(profileJSON); | |
} | |
} | |
} | |
// Final Free up of the profile JSON | |
json_decref(profileJSON); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment