int PrependListPackageItem(PackageItem ** list, char *item, Attributes a, Promise *pp) { char name[CF_MAXVARSIZE]; char arch[CF_MAXVARSIZE]; char version[CF_MAXVARSIZE]; char vbuff[CF_MAXVARSIZE]; strncpy(vbuff, ExtractFirstReference(a.packages.package_list_name_regex, item), CF_MAXVARSIZE - 1); sscanf(vbuff, "%s", name); /* trim */ strncpy(vbuff, ExtractFirstReference(a.packages.package_list_version_regex, item), CF_MAXVARSIZE - 1); sscanf(vbuff, "%s", version); /* trim */ if (a.packages.package_list_arch_regex) { strncpy(vbuff, ExtractFirstReference(a.packages.package_list_arch_regex, item), CF_MAXVARSIZE - 1); sscanf(vbuff, "%s", arch); /* trim */ } else { strncpy(arch, "default", CF_MAXVARSIZE - 1); } if (strcmp(name, "CF_NOMATCH") == 0 || strcmp(version, "CF_NOMATCH") == 0 || strcmp(arch, "CF_NOMATCH") == 0) { return false; } CfDebug(" -? Package line \"%s\"\n", item); CfDebug(" -? with name \"%s\"\n", name); CfDebug(" -? with version \"%s\"\n", version); CfDebug(" -? with architecture \"%s\"\n", arch); return PrependPackageItem(list, name, version, arch, a, pp); }
static void MonLogSymbolicValue(EvalContext *ctx, const char *handle, Item *stream, Attributes a, const Promise *pp, PromiseResult *result) { char value[CF_BUFSIZE], sdate[CF_MAXVARSIZE], filename[CF_BUFSIZE], *v; int count = 1, found = false, match_count = 0; Item *ip, *match = NULL, *matches = NULL; time_t now = time(NULL); FILE *fout; if (stream == NULL) { Log(LOG_LEVEL_VERBOSE, "No stream to measure"); return; } Log(LOG_LEVEL_VERBOSE, "Locate and log sample ..."); for (ip = stream; ip != NULL; ip = ip->next) { if (ip->name == NULL) { continue; } if (count == a.measure.select_line_number) { Log(LOG_LEVEL_VERBOSE, "Found line %d by number...", count); found = true; match_count = 1; match = ip; if (a.measure.extraction_regex) { Log(LOG_LEVEL_VERBOSE, "Now looking for a matching extractor \"%s\"", a.measure.extraction_regex); strncpy(value, ExtractFirstReference(a.measure.extraction_regex, match->name), CF_MAXVARSIZE - 1); Log(LOG_LEVEL_INFO, "Extracted value \"%s\" for promise \"%s\"", value, handle); AppendItem(&matches, value, NULL); } break; } if (a.measure.select_line_matching && StringMatchFull(a.measure.select_line_matching, ip->name)) { Log(LOG_LEVEL_VERBOSE, "Found line %d by pattern...", count); found = true; match = ip; match_count++; if (a.measure.extraction_regex) { Log(LOG_LEVEL_VERBOSE, "Now looking for a matching extractor \"%s\"", a.measure.extraction_regex); strncpy(value, ExtractFirstReference(a.measure.extraction_regex, match->name), CF_MAXVARSIZE - 1); Log(LOG_LEVEL_INFO, "Extracted value \"%s\" for promise \"%s\"", value, handle); AppendItem(&matches, value, NULL); } } count++; } if (!found) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Promiser '%s' found no matching line.", pp->promiser); *result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL); return; } if (match_count > 1) { Log(LOG_LEVEL_INFO, "Warning: %d lines matched the line_selection \"%s\"- matching to last", match_count, a.measure.select_line_matching); } switch (a.measure.data_type) { case CF_DATA_TYPE_COUNTER: Log(LOG_LEVEL_VERBOSE, "Counted %d for %s", match_count, handle); snprintf(value, CF_MAXVARSIZE, "%d", match_count); break; case CF_DATA_TYPE_STRING_LIST: v = ItemList2CSV(matches); snprintf(value, CF_BUFSIZE, "%s", v); free(v); break; default: snprintf(value, CF_BUFSIZE, "%s", matches->name); } DeleteItemList(matches); if (a.measure.history_type && strcmp(a.measure.history_type, "log") == 0) { snprintf(filename, CF_BUFSIZE, "%s%cstate%c%s_measure.log", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR, handle); if ((fout = fopen(filename, "a")) == NULL) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to open the output log \"%s\"", filename); *result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL); PromiseRef(LOG_LEVEL_ERR, pp); return; } strncpy(sdate, ctime(&now), CF_MAXVARSIZE - 1); if (Chop(sdate, CF_EXPANDSIZE) == -1) { Log(LOG_LEVEL_ERR, "Chop was called on a string that seemed to have no terminator"); } fprintf(fout, "%s,%ld,%s\n", sdate, (long) now, value); Log(LOG_LEVEL_VERBOSE, "Logging: %s,%s to %s", sdate, value, filename); fclose(fout); } else // scalar or static { CF_DB *dbp; char id[CF_MAXVARSIZE]; if (!OpenDB(&dbp, dbid_static)) { return; } snprintf(id, CF_MAXVARSIZE - 1, "%s:%d", handle, a.measure.data_type); WriteDB(dbp, id, value, strlen(value) + 1); CloseDB(dbp); } }
static PromiseResult MonExtractValueFromStream(EvalContext *ctx, const char *handle, Item *stream, Attributes a, const Promise *pp, double *value_out) { char value[CF_MAXVARSIZE]; int count = 1, found = false, match_count = 0, done = false; double real_val = 0; Item *ip, *match = NULL; bool ok_conversion = true; for (ip = stream; ip != NULL; ip = ip->next) { if (count == a.measure.select_line_number) { found = true; match = ip; match_count++; } if (a.measure.select_line_matching && StringMatchFull(a.measure.select_line_matching, ip->name)) { Log(LOG_LEVEL_VERBOSE, " ?? Look for %s regex %s", handle, a.measure.select_line_matching); found = true; match = ip; if (a.measure.extraction_regex) { switch (a.measure.data_type) { case CF_DATA_TYPE_INT: case CF_DATA_TYPE_REAL: strncpy(value, ExtractFirstReference(a.measure.extraction_regex, match->name), CF_MAXVARSIZE - 1); if (strcmp(value, "CF_NOMATCH") == 0) { ok_conversion = false; Log(LOG_LEVEL_VERBOSE, "Was not able to match a value with '%s' on '%s'", a.measure.extraction_regex, match->name); } else { if (ok_conversion) { Log(LOG_LEVEL_VERBOSE, "Found candidate match value of '%s'", value); if (a.measure.policy == MEASURE_POLICY_SUM || a.measure.policy == MEASURE_POLICY_AVERAGE) { double delta = 0; if (DoubleFromString(value, &delta)) { real_val += delta; } else { Log(LOG_LEVEL_ERR, "Error in double conversion from string value: %s", value); return false; } } else { if (!DoubleFromString(value, &real_val)) { Log(LOG_LEVEL_ERR, "Error in double conversion from string value: %s", value); return false; } } match_count++; if (a.measure.policy == MEASURE_POLICY_FIRST) { done = true; } } } break; default: Log(LOG_LEVEL_ERR, "Unexpected data type in data_type attribute: %d", a.measure.data_type); } } } count++; if (done) { break; } } if (!found) { cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_FAIL, pp, a, "Could not locate the line for promise '%s'", handle); *value_out = 0.0; return PROMISE_RESULT_FAIL; } switch (a.measure.data_type) { case CF_DATA_TYPE_COUNTER: real_val = (double) match_count; break; case CF_DATA_TYPE_INT: if (match_count > 1) { Log(LOG_LEVEL_INFO, "Warning: %d lines matched the line_selection \"%s\"- making best average", match_count, a.measure.select_line_matching); } if (match_count > 0 && a.measure.policy == MEASURE_POLICY_AVERAGE) // If not "average" then "sum" { real_val /= match_count; } break; case CF_DATA_TYPE_REAL: if (match_count > 1) { Log(LOG_LEVEL_INFO, "Warning: %d lines matched the line_selection \"%s\"- making best average", match_count, a.measure.select_line_matching); } if (match_count > 0) { real_val /= match_count; } break; default: Log(LOG_LEVEL_ERR, "Unexpected data type in data_type attribute: %d", a.measure.data_type); } if (!ok_conversion) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to extract a value from the matched line '%s'", match->name); PromiseRef(LOG_LEVEL_INFO, pp); *value_out = 0.0; return PROMISE_RESULT_FAIL; } Log(LOG_LEVEL_INFO, "Extracted value \"%f\" for promise \"%s\"", real_val, handle); *value_out = real_val; return PROMISE_RESULT_NOOP; }