/*
** SellaNMS
** $Id: snmptrap.h,v 1.36 2005/05/15 05:21:17 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_MODULE_MONITOR_SNMPTRAP_H
#define __DGS_MODULE_MONITOR_SNMPTRAP_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include "util.h"
#include "storagedata.h"
#include "policy_framework/policy_framework.h"
#include "../monitor.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#ifdef HAVE_NET_SNMP
	#include <net-snmp/net-snmp-config.h>
	#include <net-snmp/net-snmp-includes.h>
	#include <net-snmp/library/default_store.h>
#else
	#include <ucd-snmp/ucd-snmp-config.h>
	#include <ucd-snmp/ucd-snmp-includes.h>
	#include <ucd-snmp/default_store.h>
#endif
#include <stl/queue>

#define MODULE_MONITOR_SNMPTRAP_QUEUEDEPTH_DEFAULT 250
#define MODULE_MONITOR_SNMPTRAP_MAXNAMELENGTH_DEFAULT 100
#define MODULE_MONITOR_SNMPTRAP_EXITAFTER_DEFAULT 0
#define MODULE_MONITOR_SNMPTRAP_NEXTWARN_DEFAULT 60

typedef queue<snmp_pdu*> received_queue_t;
typedef struct {
   u_int32_t source;
   time_t timestamp;
   unsigned long signature;
} dup_hist_t;

class SNMPTrap: public Monitor {
	public:
		SNMPTrap();
		~SNMPTrap();

	protected:

	private:
		virtual RESULT execute();
		virtual RESULT readConfig();
		virtual RESULT printConfig();

		RESULT conv_pdu(snmp_pdu *pdu, unsigned target, bool proxy, bool skip_zero = false);
		bool suppressDuplicate(u_int32_t *source, char* msg, int n);

		static RESULT receive_thread(void *args);
		static RESULT storage_thread(void *args);
		static RESULT proxy_thread(void *args);
		static int snmptrap_callback(int op, struct snmp_session *ss, int reqid, struct snmp_pdu *pdu, void *magic);

		void cleanValue(char* buf);

		received_queue_t trap_queue;
		received_queue_t proxy_queue;

		pthread_mutex_t trap_mutex;
		pthread_mutex_t proxy_mutex;

		vector<string> proxy;

		uint32_t source;
		unsigned long port;
		unsigned int convert;
		unsigned long queueDepth;

		dup_hist_t *dupHist;
		unsigned long dupHistCur;
		unsigned long dupHistMax;
		unsigned long dupHistAge;
		unsigned long dupFuzFactor;
		unsigned long exitAfter;
		time_t nextWarning;

		int trap_flag[2];
		int proxy_flag[2];
};

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/
