1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <cf3.defs.h>
#include <known_dirs.h>
#include <eval_context.h>
#include <promises.h>
#include <probes.h>
#include <files_lib.h>
#include <files_names.h>
#include <files_interfaces.h>
#include <vars.h>
#include <item_lib.h>
#include <conversion.h>
#include <scope.h>
#include <matching.h>
#include <instrumentation.h>
#include <pipes.h>
#include <locks.h>
#include <string_lib.h>
#include <exec_tools.h>
#include <unix.h>
#include <file_lib.h>
#include <monitoring_read.h>
void NovaNamedEvent(const char *eventname, double value)
{
Event ev_new, ev_old;
time_t now = time(NULL);
CF_DB *dbp;
if (!OpenDB(&dbp, dbid_measure))
{
return;
}
ev_new.t = now;
if (ReadDB(dbp, eventname, &ev_old, sizeof(ev_old)))
{
if (isnan(ev_old.Q.expect))
{
ev_old.Q.expect = value;
}
if (isnan(ev_old.Q.var))
{
ev_old.Q.var = 0;
}
ev_new.Q = QAverage(ev_old.Q, value, 0.7);
}
else
{
ev_new.Q = QDefinite(value);
}
Log(LOG_LEVEL_VERBOSE, "Wrote scalar named event \"%s\" = (%.2lf,%.2lf,%.2lf)", eventname, ev_new.Q.q,
ev_new.Q.expect, sqrt(ev_new.Q.var));
WriteDB(dbp, eventname, &ev_new, sizeof(ev_new));
CloseDB(dbp);
}
/*****************************************************************************/
/* Level */
/*****************************************************************************/
static void Nova_DumpSlots(void)
{
#define MAX_KEY_FILE_SIZE 16384 /* usually around 4000, cannot grow much */
char filename[CF_BUFSIZE];
int i;
snprintf(filename, CF_BUFSIZE - 1, "%s%cts_key", GetStateDir(), FILE_SEPARATOR);
char file_contents_new[MAX_KEY_FILE_SIZE] = {0};
for (i = 0; i < CF_OBSERVABLES; i++)
{
char line[CF_MAXVARSIZE];
if (NovaHasSlot(i))
{
snprintf(line, sizeof(line), "%d,%s,%s,%s,%.3lf,%.3lf,%d\n",
i,
NULLStringToEmpty((char*)NovaGetSlotName(i)),
NULLStringToEmpty((char*)NovaGetSlotDescription(i)),
NULLStringToEmpty((char*)NovaGetSlotUnits(i)),
NovaGetSlotExpectedMinimum(i), NovaGetSlotExpectedMaximum(i), NovaIsSlotConsolidable(i) ? 1 : 0);
}
else
{
snprintf(line, sizeof(line), "%d,spare,unused\n", i);
}
strlcat(file_contents_new, line, sizeof(file_contents_new));
}
bool contents_changed = true;
Writer *w = FileRead(filename, MAX_KEY_FILE_SIZE, NULL);
if (w)
{
if(strcmp(StringWriterData(w), file_contents_new) == 0)
{
contents_changed = false;
}
WriterClose(w);
}
if(contents_changed)
{
Log(LOG_LEVEL_VERBOSE, "Updating %s with new slot information", filename);
if(!FileWriteOver(filename, file_contents_new))
{
Log(LOG_LEVEL_ERR, "Nova_DumpSlots: Could not write file '%s'. (FileWriteOver: %s)", filename,
GetErrorStr());
}
}
}
void GetObservable(int i, char *name, size_t name_size, char *desc, size_t desc_size)
{
Nova_LoadSlots();
if (i < ob_spare)
{
strncpy(name, OBSERVABLES[i][0], name_size - 1);
strncpy(desc, OBSERVABLES[i][1], desc_size - 1);
}
else
{
if (SLOTS[i - ob_spare])
{
strncpy(name, SLOTS[i - ob_spare]->name, name_size - 1);
strncpy(desc, SLOTS[i - ob_spare]->description, desc_size - 1);
}
else
{
strncpy(name, OBSERVABLES[i][0], name_size - 1);
strncpy(desc, OBSERVABLES[i][1], desc_size - 1);
}
}
}
void SetMeasurementPromises(Item ** classlist)
{
CF_DB *dbp;
CF_DBC *dbcp;
char eventname[CF_MAXVARSIZE], assignment[CF_BUFSIZE];
Event entry;
char *key;
void *stored;
int ksize, vsize;
if (!OpenDB(&dbp, dbid_measure))
{
return;
}
if (!NewDBCursor(dbp, &dbcp))
{
Log(LOG_LEVEL_INFO, "Unable to scan class db");
CloseDB(dbp);
return;
}
memset(&entry, 0, sizeof(entry));
while (NextDB(dbcp, &key, &ksize, &stored, &vsize))
{
if (stored != NULL)
{
if (sizeof(entry) < (size_t) vsize)
{
Log(LOG_LEVEL_ERR, "Invalid entry in measurements database. Expected size: %zu, actual size: %d", sizeof(entry), vsize);
continue;
}
strcpy(eventname, (char *) key);
memcpy(&entry, stored, MIN(vsize, sizeof(entry)));
Log(LOG_LEVEL_VERBOSE, "Setting measurement event %s", eventname);
// a.measure.data_type is not longer known here, so look for zero decimals
if ((int) (entry.Q.q * 10) % 10 == 0)
{
snprintf(assignment, CF_BUFSIZE - 1, "value_%s=%.0lf", eventname, entry.Q.q);
}
else
{
snprintf(assignment, CF_BUFSIZE - 1, "value_%s=%.2lf", eventname, entry.Q.q);
}
AppendItem(classlist, assignment, NULL);
snprintf(assignment, CF_BUFSIZE - 1, "av_%s=%.2lf", eventname, entry.Q.expect);
AppendItem(classlist, assignment, NULL);
snprintf(assignment, CF_BUFSIZE - 1, "dev_%s=%.2lf", eventname, sqrt(entry.Q.var));
AppendItem(classlist, assignment, NULL);
}
}
DeleteDBCursor(dbcp);
CloseDB(dbp);
}
/*****************************************************************************/
/* Clock handling */
/*****************************************************************************/
/* MB: We want to solve the geometric series problem to simulate an unbiased
average over a grain size for long history aggregation at zero cost, i.e.
we'd ideally like to have
w = (1-w)^n for all n
The true average is expensive to compute, so forget the brute force approach
because this gives a pretty good result. The eqn above has no actual solution
but we can approximate numerically to w = 0.01, see this test to show that
the distribution is flat:
main ()
{ int i,j;
double w = 0.01,wp;
for (i = 1; i < 20; i++)
{
printf("(");
wp = w;
for (j = 1; j < i; j++)
{
printf("%f,",wp);
wp *= (1- w);
}
printf(")\n");
}
}
*/
/*****************************************************************************/
/* Level */
/*****************************************************************************/
static int NovaGetSlot(const char *name)
{
int i;
Nova_LoadSlots();
/* First try to find existing slot */
for (i = 0; i < CF_OBSERVABLES - ob_spare; ++i)
{
if (SLOTS[i] && !strcmp(SLOTS[i]->name, name))
{
Log(LOG_LEVEL_VERBOSE, "Using slot ob_spare+%d (%d) for %s", i, i + ob_spare, name);
return i + ob_spare;
}
}
/* Then find the spare one */
for (i = 0; i < CF_OBSERVABLES - ob_spare; ++i)
{
if (!SLOTS[i])
{
Log(LOG_LEVEL_VERBOSE, "Using empty slot ob_spare+%d (%d) for %s", i, i + ob_spare, name);
return i + ob_spare;
}
}
Log(LOG_LEVEL_ERR,
"Measurement slots are all in use - it is not helpful to measure too much, you can't usefully follow this many variables");
return -1;
}
/*****************************************************************************/
int NovaRegisterSlot(const char *name, const char *description,
const char *units, double expected_minimum,
double expected_maximum, bool consolidable)
{
int slot = NovaGetSlot(name);
if (slot == -1)
{
return -1;
}
Nova_FreeSlot(SLOTS[slot - ob_spare]);
SLOTS[slot - ob_spare] = Nova_MakeSlot(name, description, units, expected_minimum, expected_maximum, consolidable);
Nova_DumpSlots();
return slot;
}
|