File: nws_sensor.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (492 lines) | stat: -rw-r--r-- 13,076 bytes parent folder | download
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/* $Id: nws_sensor.c,v 1.185 2004/12/02 04:25:49 graziano Exp $ */

#include "config_nws.h"
#include <unistd.h>           /* getopt() */

#include "nws_sensor.h"
#include "dnsutil.h"
#include "clique_protocol.h"
#include "protocol.h"
#include "host_protocol.h"
#include "diagnostic.h"
#include "messages.h"
#include "osutil.h"
#include "periodic.h"
#include "skills.h"
#include "strutil.h"
#include "nws_sensor.h"
#include "register.h"

/**
 * Search the nameserver for the registration of the memory with host
 * hostName
 */
#define LOOKUP_MEMORY_FILTER "(&(name=%s*)(hostType=memory))"
#define LOOKUP_MEMORY_FILTER_LEN 30
static int
LookupMemory(struct host_cookie *nameServer,
             const char *hostName,
             struct host_cookie *hostCookie) {
	char *filter;
	ObjectSet objSet;
	int ret;

	/* sanity check */
	if (nameServer == NULL || hostName == NULL || hostCookie == NULL) {
		ERROR("LookupMemory: NULL parameters\n");
		return 0;
	}

	/* get all objectclass=nwsHost with this name */
	objSet = NewObjectSet();
	filter = MALLOC(LOOKUP_MEMORY_FILTER_LEN + strlen(hostName) + 1);
	if (filter == NULL || objSet == NULL) {
		ABORT("LookupMemory: out of memory\n");
	}
	sprintf(filter, LOOKUP_MEMORY_FILTER, hostName);
	ret = !RetrieveObjects(nameServer, filter, &objSet, -1);
	free(filter);

	/* let's check if we got something */
	if (ret || (!ret && strlen(objSet) == 0)) {
		FreeObjectSet(&objSet);
		return 0;
	}

	/* set defaults */
	SAFESTRCPY(hostCookie->name, "");
	hostCookie->port = 0;
	hostCookie->sd = NO_SOCKET;

	/* let's try to connect to whatever we got back */
	if (ConnectToObject(NextObject(objSet, NO_OBJECT), hostCookie)) {
		/* found a good memory */
		ret = 1;
	} 

	/* free allocated memory */
	FreeObjectSet(&objSet);

	return ret;
}


/*
 * try to find the leader for clique named #cliqueName#. Put the host in
 * #cookie#. Returns 1 on success 0 otherwise.
 */
static int
FindLeader(	const char *cliqueName,
		struct host_cookie *leader) {
	char *filter;
	ObjectSet retrieved = NULL;
	Object clique = NO_OBJECT;
	int ret = 1;

	/* sanity check */
	if (leader == NULL || cliqueName == NULL) {
		ERROR("FindLeader: NULL parameters\n");
		return 0;
	}

	/* get the registrations */
	filter = (char *) MALLOC(strlen(cliqueName) + 8);
	if (filter == NULL) {
		ERROR("FindLeader: out of memory\n");
		return 0;
	}
	sprintf(filter, "(name=%s)", cliqueName);
	if (!RetrieveFromMyNameserver(filter, &retrieved)) {
		FREE(filter);
		ERROR("FindLeader: error talking with nameserver\n");
		return 0;
	}
	FREE(filter);

	/* did we get the clique? */
	for (clique = NextObject(retrieved, NULL); clique != NULL; clique = NextObject(retrieved, clique)) {
		filter = NwsAttributeValue_r(FindNwsAttribute(clique, "controlName"));
		if (filter != NULL && strcmp(filter, "clique") == 0) {
			/* found the clique */
			break;
		}
	}
	FREE(filter);
	if (clique == NULL) {
		ERROR1("FindLeader: couldn't find clique %s\n", cliqueName);
		return 0;
	}

	/* we got the clique: let's get the leader */
	filter = NwsAttributeValue_r(FindNwsAttribute(clique, "host"));
	if (filter == NULL) {
		ERROR1("FindLeader: clique registration bogus %s\n", clique);
		ret = 0;
	} else {
		/* fill the cookie */
		Host2Cookie(filter, DefaultHostPort(SENSOR_HOST), leader);
	}
	FreeObject(&retrieved);
	FREE(filter);

	return 1;

}

#define NEVER 0.0
#define SENSOR_ARGS "j:i:n:a:c:e:l:M:N:Pp:v:VS:A:fm:x:"

void usage() {
	printf("\nUsage: nws_sensor [OPTIONS]\n");
	printf("Sensor for the Network Weather Service\n");
	printf("\nOPTIONS can be:\n");
	printf("\t-e filename         write error messages to filename\n");
	printf("\t-l filename         write info/debug messages to filename\n");
	printf("\t-i filename         write pid to filename\n");
	printf("\t-M memory           use memory to store measurements\n");
	printf("\t-N nameserver       register with this nameserver\n");
	printf("\t-p port             bind to port instead of the default\n");
	printf("\t-a address          use this address as mine (ie multi-homed hosts)\n");
	printf("\t-n name             use name as my hostname\n");
	printf("\t-c [yes|no]         start CpuMonitor at startup (default yes)\n");
	printf("\t-m [yes|no]         start MemoryMonitor at startup (default no)\n");
	printf("\t-A [yes|no]         start Availability Monitor at startup (default no)\n");
	printf("\t-S [yes|no]         start process start Monitor at startup (default no)\n");
	printf("\t-P password         use password to control sensor\n");
	printf("\t-f                  don't fork (experimental)\n");
	printf("\t-x port             use port when doing network experiment (experimental)\n");
	printf("\t-j clique           join this clique (experimental)\n");
	printf("\t-v level            verbose level (up to 5)\n");
	printf("\t-V                  print version\n");
	printf("Report bugs to <nws@nws.cs.ucsb.edu>.\n\n");
}


int
main(	int argc,
	char *argv[]) {

	char opts[255+1];
	IPAddress addresses[MAX_ADDRESSES], address;
	unsigned int addressesCount;
	int opt,
		verbose = 2,			/* what to print */
		tmp;
	char *name,
		memoryName[127 + 1],		/* hostMemory name */
		password[127 + 1],
		cliqueName[MAX_CLIQUE_NAME_SIZE],
		tmpIP[127+1],
		*pidFile;
	IPAddress tmpAddress;
	struct host_cookie nsCookie,
		sensorCookie,
		memoryCookie;
	double nextBeatTime,
		nextWorkTime,
		wakeup,
		now;
	extern char *optarg;
	const char *c;
	FILE *logFD, *errFD;
	int skillToStart[SKILL_COUNT];

	/* Set up default values */
	for (tmp = 0; tmp < SKILL_COUNT; tmp++) {
		skillToStart[tmp] = 0;
	}
	skillToStart[cpuMonitor] = 1;
	logFD = errFD = stdout;
	cliqueName[0] = password[0] = tmpIP[0] = opts[0] = '\0';
	pidFile = NULL;

	/* zeroes the cookies */
	nsCookie.name[0] = sensorCookie.name[0] = memoryCookie.name[0] = '\0';
	nsCookie.port = sensorCookie.port = memoryCookie.port= 0;

	/* if we don't have a default memory, we try to look for one on
	 * this host */
	name = GetEnvironmentValue("SENSOR_MEMORY", "~", ".nwsrc", MyMachineName());
	if (name == NULL) {
		SAFESTRCPY(memoryName, "");
	} else {
		SAFESTRCPY(memoryName, name);
		FREE(name);
	}

	/* if we don't have a default nameserver, we try to look for one on
	 * this host */
	name = GetEnvironmentValue("NAME_SERVER", "~", ".nwsrc", MyMachineName());
	if (name == NULL) {
		SAFESTRCPY(nsCookie.name, "");
	} else {
		SAFESTRCPY(nsCookie.name, name);
		FREE(name);
	}
	Host2Cookie(nsCookie.name, DefaultHostPort(NAME_SERVER_HOST), &nsCookie);

	/* who am I? I think, thus I exist! */
	Host2Cookie(MyMachineName(), DefaultHostPort(SENSOR_HOST), &sensorCookie);
	addressesCount = IPAddressValues(MyMachineName(),
			&addresses[0],
			MAX_ADDRESSES);
	if (addressesCount == 0) {
		ERROR1("Couldn't resolve my name (%s)\n", MyMachineName());
	}


	/* no error messages from getopt() */
	opterr = 0;

	while((opt = getopt(argc, argv, SENSOR_ARGS)) != EOF) {
		switch(opt) {

		case 'n':
			/* let's check the user knows what is doing */
			if (IPAddressValue(optarg, &tmpAddress)) {
				/* save the inet addresses */
				addressesCount += IPAddressValues(optarg, &addresses[addressesCount], MAX_ADDRESSES - addressesCount);
			} else {
				ERROR1("Unable to resolve '%s': I'll do what you said but expect problems!\n", optarg);
			}

			/* overrride the name of the machine: we hope the
			 * user knows what is doing! */
			SAFESTRCPY(sensorCookie.name, optarg);

			break;

		case 'a':
			/* let's add this IPs to the list of my addresses */
			for (c = optarg; GETTOK(tmpIP, c, ",", &c); ) {
				tmp = IPAddressValues(tmpIP, 
						&addresses[addressesCount],
						MAX_ADDRESSES - addressesCount);
				if (tmp == 0) {
					ERROR1("Unable to convert '%s' into an IP address\n", tmpIP);
				} else {
					addressesCount += tmp;
					/* I want to have these addresses
					 * as first */
					for (; tmp > 0; tmp--) {
						address = addresses[tmp - 1];
						addresses[tmp-1] = addresses[addressesCount-tmp];
						addresses[addressesCount-tmp] = address;
					}
				}
			}
			break;

		 case 'A':
			skillToStart[availabilityMonitor] = (*optarg == 'y') || (*optarg == 'Y');
			break;
		 case 'S':
			skillToStart[startMonitor] = (*optarg == 'y') || (*optarg == 'Y');
			break;

		case 'm':
			skillToStart[memoryMonitor] = (*optarg == 'y') || (*optarg == 'Y');
			break;

		case 'c':
			skillToStart[cpuMonitor] = (*optarg == 'y') || (*optarg == 'Y');
			break;

		case 'f':
			tmp = strlen(opts);
			if (tmp == 0) {
				snprintf(opts + tmp, 255 - tmp, "fork:no");
			} else {
				snprintf(opts + tmp, 255 - tmp, "\tfork:no");
			}
			break;

		case 'e':
			/* open the error file */
			errFD = fopen(optarg, "w");
			if (errFD == NULL) {
				printf("Couldn't open %s!\n", optarg);
				exit(1);
			}
			break;

		case 'l':
			/* open the log file */
			logFD = fopen(optarg, "w");
			if (logFD == NULL) {
				printf("Couldn't open %s!\n", optarg);
				exit(1);
			}
			break;

		case 'i':
			pidFile = strdup(optarg);
			if (pidFile == NULL) {
				ABORT("out of memory!\n");
			}
			break;

		case 'j':
			SAFESTRCPY(cliqueName, optarg);
			break;

		case 'M':
			SAFESTRCPY(memoryName, optarg);
			break;

		case 'N':
			Host2Cookie(optarg, DefaultHostPort(NAME_SERVER_HOST), &nsCookie);
			break;

		case 'P':
			printf("password? ");
			scanf("%s", password);
			break;

		case 'p':
			sensorCookie.port = strtol(optarg, NULL, 10);
			break;

		case 'x':
			tmp = strlen(opts);
			if (tmp == 0) {
				snprintf(opts + tmp, 255 - tmp, "forceport:%d", (unsigned short)atol(optarg));
			} else {
				snprintf(opts + tmp, 255 - tmp, "\tforceport:%d", (unsigned short)atol(optarg));
			}
			break;

		case 'v':
			verbose = (unsigned short)atol(optarg);
			break;

		case 'V':
			printf("nws_sensor for NWS version %s", VERSION);
#ifdef HAVE_PTHREAD_H
			printf(", with thread support");
#endif
#ifdef WITH_DEBUG
			printf(", with debug support");
#endif
			printf("\n\n");
			exit(0);
			break;

		case '?':
			if (optopt == 'v') {
				/* using the first level */
				verbose = 1;
				break;
			}
			/* non-recognized options: printing help */

		default:
			usage();
			exit(1);
			break;

		}
	}

	/* let's set the verbose level  and open up files (if needed) */
	/* fatal errors are always on */
	SetDiagnosticLevel(verbose, errFD, logFD);
	
	/* let's set the memory as we are given */
	Host2Cookie(memoryName, DefaultHostPort(MEMORY_HOST), &memoryCookie);
	SAFESTRCPY(memoryName, HostCImage(&memoryCookie));

	/* let's look for the nameserver */
	if(!ConnectToHost(&nsCookie, &nsCookie.sd)) {
		WARN("Unable to contact name server\n");
	} else {
		/* we are here only if we managed to talk with the NS */
		if (!LookupMemory(&nsCookie, memoryName, &memoryCookie)) {
			WARN("Unable to find memory registration\n");
		}
	}

	/* let's get ready to listen */
	if(!EstablishHost(HostCImage(&sensorCookie),
                    SENSOR_HOST,
                    addresses,
                    addressesCount,
                    sensorCookie.port,
                    password,
                    &nsCookie,
                    &memoryCookie,
                    &NwsSensorExit)) {
		FAIL("Unable to establish host: port already in use?\n");
	}

	/* now that we've got the port, we can print the pid into the
	 * pid file. We thus avoid to override pid files of running
	 * nameservers */
	if (pidFile != NULL) {
		FILE *pidfile = fopen(pidFile, "w");
		if (!pidfile) {
			ABORT1("Can't write pidfile %s\n", pidFile);
		}
		fprintf(pidfile, "%d", (int)getpid());
		fclose(pidfile);
		free(pidFile);
	}

	/* initialize the sensor */
	if (!NwsSensorInit(opts)) {
		WARN("Sensor: failed to init the sensors\n");
	}

	for (tmp = 0; tmp < SKILL_COUNT; tmp++) {
		if (!skillToStart[tmp]) {
			continue;
		}
		/* start the activity with the default name */
		if(!StartPeriodicActivity(NULL, SkillName(tmp), "")) {
			WARN1("StartSensor: auto-start of %s failed\n", SkillName(tmp));
		}
		FREE(name);
	}

	/* have we been asked to join a clique? */
	if (cliqueName[0] != '\0') {
		struct host_cookie k;

		/* yep let's join the clique: first of all find the
		 * leader of the clique */
		if (FindLeader(cliqueName, &k)) {
			/* then ask it to join the club */
			CliqueJoin(&k, cliqueName, HostCImage(&sensorCookie), -1);
		}
	}

	nextBeatTime = 0;

	while(1) {
		/* time to register again with the name server */
		now = CurrentTime();
		if(now >= nextBeatTime) {
			if (RegisterHost(DEFAULT_HOST_BEAT * 2) == 0) {
				INFO("nws_sensor: couldn't register\n");
			}
			nextBeatTime = now + (HostHealthy() ? DEFAULT_HOST_BEAT : SHORT_HOST_BEAT);
		}

		/* let's see when we need to wake up next */
		nextWorkTime = NwsSensorNextWork();
		if((nextWorkTime != NEVER) && (nextWorkTime < nextBeatTime)) {
			/* if now > wakeup ListenForMessage will just
			 * poll for messages. */
			wakeup = nextWorkTime - now;
		} else {
			wakeup = nextBeatTime - now;
		}

		ListenForMessages((wakeup > 0) ? wakeup : -1);

		/* time to do some work? NwsSensorWork will check if it's
		 * the right time to do work */
		NwsSensorWork();
	}
	return 0; /* never reached */
}