/* Init a single machine. * Extracts the machine name and assigns the correct * component type for it */ int machine_single_init (machine_t *machine) { int rc = -1; char *url; asprintf (&url, "%s%s", machine_detail_base_url, machine->uuid); chunk_t chunk; chunk.data = malloc (1); chunk.size = 0; int res = fetch_curl (url, &chunk); if (res < 0) { printf ("ERROR: Could not fetch machine detail during machine init\n"); return rc; } json_object *jdetail = json_tokener_parse (chunk.data); json_object *tmp = NULL; json_object_object_get_ex (jdetail, "name", &tmp); const char *name = json_object_get_string (tmp); if (name == NULL) { printf ("ERROR: Name of machine could not be dervied\n"); return rc; } machine->name = strdup (name); //printf ("Name of the machine: %s\n", name); if (strstr (name, "DMC")) { machine->type = CMP_DMG_DMC; } else if (strstr (name, "DMU")) { machine->type = CMP_DMG_DMU; } else if (strstr (name, "NTX")) { machine->type = CMP_DMG_NTX; } else if (strstr (name, "NZX") != NULL) { machine->type = CMP_DMG_NZX; } else if (strstr (name, "A7")) { machine->type = CMP_KASOTEC_A7; } else if (strstr (name, "A13")) { machine->type = CMP_KASOTEC_A13; } else if (strstr (name, "WSS")) { machine->type = CMP_PERNDORFER_WSS; } else if (strstr (name, "3000")) { machine->type = CMP_TRUMPF_3000; } else if (strstr (name, "7000")) { machine->type = CMP_TRUMPF_7000; } else if (strstr (name, "Lasertec")) { machine->type = CMP_DMG_LASTERTEC; } else { printf ("ERROR: Name could not be found in list\n"); return rc; } free (chunk.data); free (jdetail); rc = 0; return rc; }
/* * fetch an ASN.1 blob coded in PEM or DER format from a URL */ static err_t fetch_asn1_blob(char *url, chunk_t *blob) { err_t ugh = NULL; if (strlen(url) >= 4 && strncasecmp(url, "ldap", 4) == 0) { ugh = fetch_ldap_url(url, blob); } else { ugh = fetch_curl(url, blob); } if (ugh != NULL) return ugh; if (is_asn1(*blob)) { DBG(DBG_PARSING, DBG_log(" fetched blob coded in DER format") ) } else { bool pgp = FALSE; ugh = pemtobin(blob, NULL, "", &pgp); if (ugh == NULL) { if (is_asn1(*blob)) { DBG(DBG_PARSING, DBG_log(" fetched blob coded in PEM format") ) } else { ugh = "blob coded in unknown format"; pfree(blob->ptr); } } else { pfree(blob->ptr); } }
/* Get the sensor readings and * the local time at the machine site */ int get_sensor_readings (sensor_t *sensor, struct tm *tm) { int rc = -1; /* init chunk */ chunk_t chunk; chunk.data = malloc (1); chunk.size = 0; rc = fetch_curl (env_sensor_url, &chunk); if ((rc < 0) || (chunk.size == 0)) { fprintf (stderr, "fetching sensor details failed\n"); return rc; } json_object *jdetail = json_tokener_parse (chunk.data); json_object *jtemp = NULL, *jpres = NULL, *jhumd = NULL; json_object_object_get_ex (jdetail, "temperature", &jtemp); json_object_object_get_ex (jdetail, "pressure", &jpres); json_object_object_get_ex (jdetail, "humidity", &jhumd); /* update the period window */ if (sensor->size == pwindow_size) { printf ("ERROR: phead on window_size in sensor. buffer needs clear up\n"); return rc; } sensor->temperature[sensor->size] = json_object_get_double (json_object_array_get_idx (jtemp, 1)); sensor->pressure[sensor->size] = json_object_get_double (json_object_array_get_idx (jpres, 1)); sensor->humidity[sensor->size] = json_object_get_double (json_object_array_get_idx (jhumd, 1)); sensor->size += 1; /* get time */ const char *time_str = json_object_get_string (json_object_array_get_idx (jtemp, 0)); strptime (time_str, "%Y-%m-%dT%H:%M:%S", tm); //printf ("Sensor data = %s and time = %s and timeepoch = %ld\n", chunk.data, time_str, mktime(tm)); /* free memory */ free (chunk.data); free (jdetail); rc = 0; return rc; }
int monitor_machine (machine_t *machine) { int rc = -1; char *url; /* create the machine url and init chunk */ asprintf (&url, "%s%s", machine_detail_base_url, machine->uuid); chunk_t chunk; chunk.data = malloc (1); chunk.size = 0; /* fetch the new machine data */ rc = fetch_curl (url, &chunk); if ((rc < 0) || (chunk.size == 0)) { printf ("fetching machine detail for machine %s failed\n", machine->uuid); return rc; } /* fetch current and current alert */ json_object *jdetail = json_tokener_parse (chunk.data); json_object *tmp = NULL; json_object_object_get_ex (jdetail, "current", &tmp); if (tmp == NULL) { printf ("ERROR: Could not get current for machine %s\n", machine->uuid); return rc; } machine->current_cur = json_object_get_double (tmp); json_object_object_get_ex (jdetail, "current_alert", &tmp); if (tmp == NULL) { printf ("ERROR: Could not get current_alert for machine %s\n", machine->uuid); } machine->current_threshold = json_object_get_double (tmp); //printf ("machine = %s, current = %f, current_alert = %f\n", machine->uuid, machine->current_cur, machine->current_threshold); /* Implementation with timestamp for each window entry */ /* send alert if current is greater than threshold */ int64_t timenow = epochtime (); if (machine->current_cur > machine->current_threshold) { int i = 0; double sum = 0, avg = 0; int count = 0; int head_dup = machine->head - 1; if (head_dup < 0) head_dup = window_size - 1; while ((machine->current_avgwindow[head_dup].timestamp != 0) && (machine->current_avgwindow[head_dup].timestamp > timenow - seconds_history) && (head_dup != machine->head)) { sum += machine->current_avgwindow[head_dup].current; count++; head_dup -= 1; if (head_dup < 0) head_dup = window_size - 1; } if (count > 0) avg = sum / count; else avg = machine->current_avgwindow[head_dup].current; send_alert (machine, avg); } /* update the average window */ machine->current_avgwindow[machine->head].current = machine->current_cur; machine->current_avgwindow[machine->head].timestamp = timenow; machine->head = (machine->head == window_size - 1) ? 0 : machine->head + 1; /* update the period window */ if (machine->phead == pwindow_size) { printf ("ERROR: phead on window_size. buffer needs clear up\n"); return rc; } machine->current_periodwindow[machine->phead].current = machine->current_cur; machine->phead++; /* free memory */ json_object_put (jdetail); free (chunk.data); rc = 0; return rc; }
/* machines_init() * Initializes the machine and senor data by fetching from the URL */ int machines_init (machine_t machines[], sensor_t *sensor) { int i = 0; int rc = -1; json_object *mlist; int len = 0; /* init the chunk */ chunk_t chunk; chunk.data = malloc (1); chunk.size = 0; /* Fetch the data */ rc = fetch_curl (machine_list_url, &chunk); if ((rc < 0) || (chunk.size == 0)) { printf ("fetching machine list failed\n"); return rc; } rc = -1; /* parse and create json */ mlist = json_tokener_parse (chunk.data); len = json_object_array_length (mlist); if (len == 0) { printf ("FATAL ERROR: machine list is not an array\n"); return rc; } window_size = (int)ceil((1/frequency)*seconds_history); pwindow_size = (int)ceil(PERIOD_SHORT*60*60 / frequency); printf ("Window size to be created = %d\n", (int)ceil((1/frequency)*seconds_history)); /* iterate and store machine uuids */ for (i = 0; i < len; i++) { const char *mstr = json_object_get_string (json_object_array_get_idx (mlist, i)); strncpy (machines[i].uuid, &mstr[18], 36); machines[i].uuid[36] = '\0'; machines[i].current_cur = 0; machines[i].current_threshold = 0; machines[i].current_avgwindow = (cw_t *) malloc (sizeof (cw_t) * window_size); memset (machines[i].current_avgwindow, 0, sizeof(cw_t) * window_size); machines[i].current_periodwindow = (cw_t *) malloc (sizeof (cw_t) * (pwindow_size + 1)); memset (machines[i].current_periodwindow, 0, sizeof(cw_t) * pwindow_size); machines[i].head = 0; machines[i].phead = 0; } /* Fetch all the machine names/types */ for (i = 0; i < NUM_TOTAL; i++) { rc = machine_single_init (&machines[i]); if (rc < 0) { printf ("ERROR: Could not init machine i: %d\n", i); } } /* Allocate memory for sensor data */ sensor->pressure = (double *) malloc (sizeof (double) * (pwindow_size + 1)); sensor->temperature = (double *) malloc (sizeof (double) * (pwindow_size + 1)); sensor->humidity = (double *) malloc (sizeof (double) * (pwindow_size + 1)); /* free memory */ json_object_put (mlist); free (chunk.data); printf ("Initiation Complete\n"); rc = 0; return rc; }