
#ifndef _SYSTEMSTATUS_H
#define _SYSTEMSTATUS_H

#include <stdlib.h>
#include <fcntl.h>
#include <process.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/disk.h>
#include <sys/osinfo.h>
#include <sys/psinfo.h>
#include <sys/stat.h>
#include <sys/vc.h>
#include <sys/kernel.h>

#include </usr/include/syslog.h>

#include "Syslog.h"
#include "System.h"
#include "BooleanAttribute.h"
#include "IntegerAttribute.h"
#include "AttributeParser.h"

class SystemStatus {
public:

	enum Status {
		Ok = 0,
		MaxProcesses,
		MinFreeMemory,
		MinFreeStorage
	};

	struct status {
		int numProcesses[10];
		int freeMemory[10];
		int freeSpace[10];
		Boolean supervisorRunning;
		Boolean dataValid;

		status() {
			memset( numProcesses, 0, sizeof(int) * 10 );
			memset( freeMemory, 0, sizeof(int) * 10 );
			memset( freeSpace, 0, sizeof(int) * 10 );
			supervisorRunning = False;
			dataValid=False;
		};
	};

	SystemStatus();
	~SystemStatus();

	// A run loop that periodically checks the system status,
	// prints it out and takes any appropriate action.
	void run();

	// Check the system status. Store the results in the object,
	// and return an overall status.
	int checkSystemStatus( struct status *stat = NULL);

	// Print out the system status to the screen, the vehicle log,
	// and the system log.
	void printSystemStatus();

protected:
	// The configuration parameters.
	short _numNodes;
	long _maxProcesses, _minFreeMemoryMB, _minFreeStorageMB;
	short _samplePeriod;
	Boolean _shutdownOnError;

	// Load the configuration from the config file.
	Boolean loadConfig();

	// Actually retrieve the status via system calls.
	void getSystemStatus();

	// Once we've gotten the latest values, check them
	// against the configuration parameters.
	int getOverallStatus();

	// Actually take action in the event of an error
	void handleError();

	// Count processes on a node
	int getProcessCount( short node );

	// Get free memory on a node
	int getFreeMemory( short node );

	// Get free storage space on a node
	int getFreeSpace( short node );

	// Find out if the supervisor driver/server pair is still running
	Boolean getSupervisorStatus();

	struct status _status;

	Boolean debug;

};

#endif
