コード例 #1
0
ファイル: trapHandler.cpp プロジェクト: DYFeng/infinidb
/*****************************************************************************************
* @brief	main function
*
* purpose:	Parse incoming ALARM statement into calAlarm class
*
*****************************************************************************************/
int main (int argc, char *argv[]) {

	if (argc != 2)
  		exit (0);
  
  	Alarm calAlarm;

  	char buf[100];
  	char* alarmData;
  	char* token;
  	bool successFlag = false;

  	if (::DEBUG) {
		LoggingID lid(11);
		MessageLog ml(lid);
		Message msg;
		Message::Args args;
		args.add("trapHandler Launched");
		msg.format(args);
		ml.logDebugMessage(msg);
	}

	// read alarm data
	while (cin.getline(buf,100))
  	{
		// Alarm data
		if (::DEBUG) {
			LoggingID lid(11);
        		MessageLog ml(lid);
        		Message msg;
        		Message::Args args;
        		args.add("Alarm Data:");
			args.add(buf);
        		msg.format(args);
        		ml.logDebugMessage(msg);
		}
  		// search for CALALARM
  		if ((alarmData = strstr(buf, "CALALARM")) == NULL)
  			continue;
  		
  		successFlag = true;
  		token = strtok (alarmData, DELIM);
  		
  		// alarmData format: CALALARM|alarmID|componentID|1(set)/0(clear)|server|process
  		// alarmID
		try {
  			token = getNextToken();
		} catch (runtime_error& e)
		{
			if (::DEBUG) {                                
				LoggingID lid(11);                                
				MessageLog ml(lid);                                
				Message msg;                                
				Message::Args args;                                
				args.add("getNextToken error:");                                
				args.add(e.what());                                
				msg.format(args);                                
				ml.logDebugMessage(msg);                                
			}
			exit(1);
		}

  		calAlarm.setAlarmID (atoi(token));
  		
  		// componentID
		try {
  			token = getNextToken();
		} catch (runtime_error& e)
		{
			if (::DEBUG) {                                
				LoggingID lid(11);                                
				MessageLog ml(lid);                                
				Message msg;                                
				Message::Args args;                                
				args.add("getNextToken error:");                                
				args.add(e.what());                                
				msg.format(args);                                
				ml.logDebugMessage(msg);                                
			}
			exit(1);
		}
  		calAlarm.setComponentID (token);
  		
  		// state
		try {
  			token = getNextToken();
		} catch (runtime_error& e)
		{
			if (::DEBUG) {                                
				LoggingID lid(11);                                
				MessageLog ml(lid);                                
				Message msg;                                
				Message::Args args;                                
				args.add("getNextToken error:");                                
				args.add(e.what());                                
				msg.format(args);                                
				ml.logDebugMessage(msg);                                
			}
			exit(1);
		}
  		calAlarm.setState (atoi(token));
  		
		// sname
		try {
  			token = getNextToken();
		} catch (runtime_error& e)
		{
			if (::DEBUG) {                                
				LoggingID lid(11);                                
				MessageLog ml(lid);                                
				Message msg;                                
				Message::Args args;                                
				args.add("getNextToken error:");                                
				args.add(e.what());                                
				msg.format(args);                                
				ml.logDebugMessage(msg);                                
			}
			exit(1);
		}
		calAlarm.setSname (token);

		// pname
		try {
  			token = getNextToken();
		} catch (runtime_error& e)
		{
			if (::DEBUG) {                                
				LoggingID lid(11);                                
				MessageLog ml(lid);                                
				Message msg;                                
				Message::Args args;                                
				args.add("getNextToken error:");                                
				args.add(e.what());                                
				msg.format(args);                                
				ml.logDebugMessage(msg);                                
			}
			exit(1);
		}
		calAlarm.setPname (token);

  		// distinguish agent trap and process trap.
  		// agent trap set pid and tid 0.
  		if (strcmp (argv[1], AGENT_TRAP) == 0)
  		{
  			calAlarm.setPid (0);
  			calAlarm.setTid (0);
  		}
  		// process trap continues to get pid from alarm data
  		else
  		{
  			// pid
			try {
				token = getNextToken();
			} catch (runtime_error& e)
			{
				if (::DEBUG) {                                
					LoggingID lid(11);                                
					MessageLog ml(lid);                                
					Message msg;                                
					Message::Args args;                                
					args.add("getNextToken error:");                                
					args.add(e.what());                                
					msg.format(args);                                
					ml.logDebugMessage(msg);                                
				}
				exit(1);
			}
			calAlarm.setPid (atoi(token));

  			// tid
			try {
				token = getNextToken();
			} catch (runtime_error& e)
			{
				if (::DEBUG) {                                
					LoggingID lid(11);                                
					MessageLog ml(lid);                                
					Message msg;                                
					Message::Args args;                                
					args.add("getNextToken error:");                                
					args.add(e.what());                                
					msg.format(args);                                
					ml.logDebugMessage(msg);                                
				}
				exit(1);
			}
  			calAlarm.setTid (atoi(token)); 
  		}

  		if (::DEBUG){
        		LoggingID lid(11);
        		MessageLog ml(lid);
        		Message msg;
        		Message::Args args;
        		args.add("Alarm Info:");
			args.add(calAlarm.getAlarmID());
			args.add(calAlarm.getComponentID());
			args.add(calAlarm.getState());
        		msg.format(args);
        		ml.logDebugMessage(msg);

  			cout << calAlarm.getAlarmID() << ":" 
  				 << calAlarm.getComponentID() << ":"
  				 << calAlarm.getState() << endl;
		}
  		// break while loop. ignore the other info carried by
  		// the trap. May need to retrieve more info in the future.
  		break;
	}
  
  // not valid alarm data if no "CALALARM" found
  if (!successFlag){
	LoggingID lid(11);                
	MessageLog ml(lid);                
	Message msg;                
	Message::Args args;                
	args.add("Error: not valid alarm data if no 'CALALARM' found");                
	msg.format(args);                
	ml.logDebugMessage(msg); 
	exit(1);
  }

  // Get alarm configuration
	try {
  		configAlarm (calAlarm);
	} catch (runtime_error& e)
	{
		if (::DEBUG) {                                
			LoggingID lid(11);                                
			MessageLog ml(lid);                                
			Message msg;                                
			Message::Args args;                                
			args.add("configAlarm error:");                                
			args.add(e.what());                                
			msg.format(args);                                
			ml.logDebugMessage(msg);                                
		}
		exit(1);
	}

  return 0;
}
コード例 #2
0
ファイル: snmpmanager.cpp プロジェクト: DYFeng/infinidb
/*****************************************************************************************
* @brief	getAlarm API
*
* purpose:	Get List of Historical Alarms from alarm file
*
*			date = MM/DD/YY format
*
*****************************************************************************************/
void SNMPManager::getAlarm(std::string date, AlarmList& alarmList) const
{
	string alarmFile = "/tmp/alarms";

	//make 1 alarm log file made up of archive and current alarm.log
	(void)system("touch /tmp/alarms");

	string cmd = ("ls " + ALARM_ARCHIVE_FILE + " | grep 'alarm.log' > /tmp/alarmlogfiles");
	(void)system(cmd.c_str());

	string fileName = "/tmp/alarmlogfiles";

	ifstream oldFile (fileName.c_str());
	if (oldFile) {
		char line[200];
		string buf;
		while (oldFile.getline(line, 200))
		{
			buf = line;
			string cmd = "cat " + ALARM_ARCHIVE_FILE + "/" + buf + " >> /tmp/alarms";
			(void)system(cmd.c_str());
		}
	
		oldFile.close();
		unlink (fileName.c_str());
	}

	cmd = "cat " + ALARM_FILE + " >> /tmp/alarms";
	(void)system(cmd.c_str());

   	int fd = open(alarmFile.c_str(),O_RDONLY);
	
   	if (fd == -1)
 		// doesn't exist yet, return
		return;

	ifstream hisAlarm (alarmFile.c_str(), ios::in);

	// acquire read lock
	if (flock(fd,LOCK_SH) == -1) 
	{
     	throw runtime_error ("Lock alarm log file error");
	}

	//get mm / dd / yy from incoming date
	string mm = date.substr(0,2);
	string dd = date.substr(3,2);
	string yy = date.substr(6,2);

	Alarm alarm;
	
	while (!hisAlarm.eof())
	{
		hisAlarm >> alarm;
		if (alarm.getAlarmID() != INVALID_ALARM_ID) {
			time_t cal = alarm.getTimestampSeconds();
			struct tm tm;
			localtime_r(&cal, &tm);
			char tmp[3];
			strftime (tmp, 3, "%m", &tm);
			string alarm_mm = tmp;
			alarm_mm = alarm_mm.substr(0,2);
			strftime (tmp, 3, "%d", &tm);
			string alarm_dd = tmp;
			alarm_dd = alarm_dd.substr(0,2);
			strftime (tmp, 3, "%y", &tm);
			string alarm_yy = tmp;
			alarm_yy = alarm_yy.substr(0,2);

			if ( mm == alarm_mm && dd == alarm_dd && yy == alarm_yy )
				//don't sort
	//			alarmList.insert (AlarmList::value_type(alarm.getAlarmID(), alarm));
				alarmList.insert (AlarmList::value_type(INVALID_ALARM_ID, alarm));
		}
	}
	hisAlarm.close();
	unlink (alarmFile.c_str());
	
	// release lock
	if (flock(fd,LOCK_UN) == -1) 
	{
     	throw runtime_error ("Release lock active alarm log file error");
	}
	
	if (CALPONT_SNMP_DEBUG)
	{
		AlarmList :: iterator i;
		for (i = alarmList.begin(); i != alarmList.end(); ++i)
		{
			cout << i->second << endl;
		}
	}
}