init ems server code

This commit is contained in:
2024-09-27 15:39:34 +08:00
parent 9d4009aaca
commit 9930e4e58f
1551 changed files with 110216 additions and 102864 deletions

309
plat/snmp/src/heartbeat.c Normal file
View File

@@ -0,0 +1,309 @@
/*****************************************************/
/*Title: heartbeat.c */
/*Descr: Generate flatform's heartbeart */
/*Author: Liang Hanxi */
/*Create: 2002-06-24 */
/*Modify: */
/*****************************************************/
#include "./include/heartbeat.h"
// by simon at 23/9/25
DWORD ExistIP[64];
DWORD ExistIPNum;
heartbeat_shm *HbShm=NULL;
static DWORD ObjID[20]=FLATFORM_HB_OID;
static snmp_addr HBAddr;
static BYTE InitOnce=0;
static DWORD latestAlmCmp=0;
static BYTE latestAlmCod=0;
static void send_alarm();
static void send_heartbeat();
void heartbeat_init(u_short sys_id)
{
int shmid;
struct shmid_ds shm_info;
memset(&shm_info, 0, sizeof(struct shmid_ds));
if(InitOnce == 0)
{
ExistIPNum = 0;
HBAddr.local_port = HBAddr.remote_port = HB_PORT;
HBAddr.remote_ip = inet_addr(HB_IP);
HBAddr.broadcast = 1;
sysctrl_init();
}
if(HbShm==NULL)
{
/*open share memory*/
if((shmid=shmget(HEARTBEAT_SHM_KEY,sizeof(heartbeat_shm),HEARTBEAT_SHM_PERM))==-1)
{
if((shmid=shmget(HEARTBEAT_SHM_KEY,sizeof(heartbeat_shm),HEARTBEAT_SHM_PERM|IPC_CREAT))==-1)
{
printf("fail to open share memory");
exit(1);
}
}
if((HbShm=(heartbeat_shm *)shmat(shmid,0,0))==(heartbeat_shm *)-1)
{
printf("fail to attach share memory");
exit(1);
}
shmctl(shmid,IPC_STAT,&shm_info);
if(shm_info.shm_nattch==1)
{
memset(HbShm,0,sizeof(heartbeat_shm));
HbShm->msg.sys_id=0xFF;
HbShm->msg.subsys_id=0xFF;
}
}
if((sys_id & 0xFF)!=0xFF)
{
HbShm->msg.sys_id =HbShm->sys_id=sys_id>>8;
HbShm->msg.subsys_id =HbShm->subsys_id=sys_id & 0xFF;
snmp_debug_hb(2);
}
InitOnce=1;
}
void heartbeat_timer() //called per 20ms
{
static time_t l_oldtime = 0,l_time;
static int counter=0;
l_time = time(NULL);
if(l_time - l_oldtime >= 10)
counter = 500;
system_report();
switch(counter)
{
case 500:
send_heartbeat();
counter=0;
l_oldtime = l_time;
break;
default:
if(HbShm->alarm_num >0)
{
//system_report();
send_alarm();
counter=0;
}
else
counter++;
break;
}
}
void send_heartbeat()
{
int i;
short len,msglen=0;
snmp_pdu pdu;
DWORD curtime;
curtime=htonl(time(NULL));
memcpy(HbShm->msg.timestamp,(BYTE *)&curtime,4);
for(i=0;i<8;i++)
HbShm->msg.led_color[i]=(HbShm->led[2*i]<<4) | HbShm->led[2*i+1];
for(i=0;i<COMPONENT_NUM;i++)
{
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=0;
#endif
HbShm->msg.info[msglen++]=i;
len=HbShm->component[i].status_len;
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=len >> 8;
#endif
HbShm->msg.info[msglen++]=len & 255;
memcpy(HbShm->msg.info+msglen,HbShm->component[i].status,len);
msglen+=len;
}
#if _CDMA_SNMP
HbShm->msg.length[0] = msglen >> 8;
HbShm->msg.length[1] = msglen & 255;
msglen++;
msglen+=19;
#else
HbShm->msg.length =msglen;
msglen+=18;
#endif
#if _CDMA_SNMP
/* daniel modify on 2005-7-24 */
HbShm->msg.component_id[0]=latestAlmCmp >> 8;
HbShm->msg.component_id[1]=latestAlmCmp & 255;
/* daniel modify on 2005-7-24 */
#else
HbShm->msg.component_id=latestAlmCmp;
#endif
HbShm->msg.alarm_code =latestAlmCod;
/*send out---->*/
pdu.error_index =pdu.error_status =0;
pdu.pdu_type =PDU_TRAP;
pdu.request_id =0;
memcpy(pdu.var[0].msg,(BYTE *)&HbShm->msg,msglen);
pdu.var_num=1;
pdu.var[0].msglen =msglen;
pdu.var[0].vartype =4;
memcpy(pdu.var[0].oid,ObjID,14*sizeof(DWORD));
pdu.var[0].oidlen =14;
HBAddr.local_port =HBAddr.remote_port =HB_PORT;
HBAddr.remote_ip =inet_addr(HB_IP);
for(i = 0; i < ExistIPNum; i++)
{
HBAddr.remote_ip = ExistIP[i];
HBAddr.broadcast = 0;
snmp_plat_send(&pdu, &HBAddr);
}
}
void send_alarm()
{
int i;
short len,msglen=0;
snmp_pdu pdu;
DWORD curtime;
curtime=htonl(time(NULL));
memcpy(HbShm->msg.timestamp,(BYTE *)&curtime,4);
for(i=0;i<8;i++)
HbShm->msg.led_color[i]=(HbShm->led[2*i]<<4) | HbShm->led[2*i+1];
if(HbShm->alarm_num<=0)
return;
for(i=0;i<ALARM_COMPONENT_NUM;i++)
{
if(HbShm->alarm[i].flag)
{
#if _CDMA_SNMP
HbShm->msg.component_id[0] = i >> 8;
HbShm->msg.component_id[1] = i & 255;
#else
HbShm->msg.component_id= i;
#endif
HbShm->msg.alarm_code =HbShm->alarm[i].alarm_code;
HbShm->alarm[i].flag =0;
/* daniel add on 2005-7-24 */
latestAlmCmp = i;
latestAlmCod = HbShm->msg.alarm_code;
/* daniel add on 2005-7-24 */
break;
}
}
for(i=0;i<COMPONENT_NUM;i++)
{
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=0;
#endif
HbShm->msg.info[msglen++]=i;
len=HbShm->component[i].status_len;
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=len >> 8;
#endif
HbShm->msg.info[msglen++]=len&255;
memcpy(HbShm->msg.info+msglen,HbShm->component[i].status,len);
msglen+=len;
}
#if _CDMA_SNMP
HbShm->msg.length[0] = msglen >> 8;
HbShm->msg.length[1] = msglen & 255;
msglen++;
msglen+=19;
#else
HbShm->msg.length =msglen;
msglen+=18;
#endif
if(HbShm->alarm_num >=1)
HbShm->alarm_num--;
/*send out---->*/
pdu.error_index =pdu.error_status =0;
pdu.pdu_type =PDU_TRAP;
pdu.request_id =0;
memcpy(pdu.var[0].msg,(BYTE *)&HbShm->msg,msglen);
pdu.var_num=1;
pdu.var[0].msglen =msglen;
pdu.var[0].vartype =4;
memcpy(pdu.var[0].oid,ObjID,14*sizeof(DWORD));
pdu.var[0].oidlen =14;
HBAddr.local_port =HBAddr.remote_port =HB_PORT;
HBAddr.remote_ip =inet_addr(HB_IP);
for(i = 0; i < ExistIPNum; i++)
{
HBAddr.broadcast = 0;
HBAddr.remote_ip = ExistIP[i];
snmp_plat_send(&pdu, &HBAddr);
}
}
void set_led(BYTE nled,BYTE ncolor)
{
if(HbShm==NULL)
heartbeat_init(0xFF);
HbShm->led[nled]=ncolor;
}
void set_alarm(WORD component_id,BYTE alarm_code)
{
if(HbShm==NULL)
heartbeat_init(0xFF);
if(component_id>ALARM_COMPONENT_NUM)
{
snmp_debug(SNMPDB_ALARM | SNMPDB_ERR, "Fail to send Alarm:No Such component id(%d)",component_id);
return;
}
snmp_debug(SNMPDB_ALARM, "set alarm.component id=%d,alarm_code=%d",component_id,alarm_code);
if(HbShm->alarm[component_id].alarm_code !=alarm_code)
{
HbShm->alarm[component_id].flag=1;
HbShm->alarm_num ++;
}
HbShm->alarm[component_id].alarm_code =alarm_code;
}
void set_status(BYTE component_id,BYTE info_len,BYTE *info)
{
if(HbShm == NULL)
heartbeat_init(0xFF);
if(component_id > COMPONENT_NUM)
{
snmp_debug(SNMPDB_ERR, "Fail to set status of component %d:No Such component id",component_id);
return;
}
/*
if(info_len>=256)
{
snmp_debug(SNMPDB_ERR, "Fail to set status of component %d:the length(%d) of status part exceed limit(256Byte)",component_id,info_len);
return;
}*/
HbShm->component[component_id].status_len =info_len;
memcpy(HbShm->component[component_id].status,info,info_len);
}

View File

@@ -0,0 +1,87 @@
/*************************************************/
/*Title: heartbeat.h */
/*Descr: heartbeat processor(Head File) */
/*Author: Liang Hanxi */
/*Create: 2002-4-5 */
/*Modify: 2002-6-25 */
/*************************************************/
#ifndef __HEART_BEAT__H
#define __HEART_BEAT__H
#include "snmp.h"
#include "time.h"
#include "sysctrl.h"
#define HEARTBEAT_SHM_KEY 0x01112233
#define HEARTBEAT_SHM_PERM 0666
#define COMPONENT_NUM 10
#define ALARM_COMPONENT_NUM 1024
#define LED_NUM 16
#define FLATFORM_HB_OID {1,3,6,1,4,1,1373,2,3,2,0}
#define FLATFORM_HB_OIDLEN 11
#define HB_PORT 4957
#define HB_IP "172.18.255.255"
#define INFO_LEN 1024
#define MasterSubSysID 0
#define MasterSysID 0
// by simon at 23/9/25
//DWORD ExistIP[64];
//DWORD ExistIPNum;
typedef struct component_buf{
BOOL flag;
BYTE alarm_code;
BYTE status_len;
BYTE status[256];
}component_buf;
typedef struct alarm_buf{
BOOL flag;
BYTE alarm_code;
}alarm_buf;
typedef struct heartbeat_msg{
BYTE sys_id;
BYTE subsys_id;
BYTE timestamp[4];
BYTE led_color[8];
#if _CDMA_SNMP
BYTE component_id[2];
#else
BYTE component_id;
#endif
BYTE alarm_code;
#if _CDMA_SNMP
BYTE length[2];
#else
BYTE reserved;
BYTE length;
#endif
BYTE info[INFO_LEN];
}heartbeat_msg;
typedef struct heartbeat_shm{
BYTE sys_id;
BYTE subsys_id;
BYTE led[LED_NUM];
component_buf component[COMPONENT_NUM];
alarm_buf alarm[ALARM_COMPONENT_NUM];
BYTE alarm_num;
heartbeat_msg msg;
}heartbeat_shm;
void set_led(BYTE nled,BYTE ncolor);
void set_alarm(WORD component_id,BYTE alarm_code);
void set_status(BYTE component_id,BYTE info_len,BYTE *info);
void heartbeat_init(u_short sys_id);
void heartbeat_timer();
#endif

View File

@@ -0,0 +1,100 @@
#ifndef _SNMP_MACRO_H
#define _SNMP_MACRO_H
#define SyntaxInteger 0x02
#define SyntaxOctetString 0x04
#define SyntaxIpAddress 0x40
#define SyntaxCounter 0x41
#define SyntaxTimeTicks 0x43
#define SyntaxOpaque 0x44
#define Create_Object_Cloumn 1
#define Create_Sub_Object_Command 1
#define GET_NULL() do { \
*pdata = 0; \
*vartype = SyntaxInteger; \
datalen = 1; \
} while(0)
#define GET_INTEGER(integer) do { \
*((int *)pdata) = htonl(integer); \
*vartype = SyntaxInteger; \
datalen = 4; \
} while(0)
#define SET_INTEGER(integer) do { \
int i; \
if(datalen > 4) \
return -1; \
for(i = 0, integer = 0; i < datalen; i++) \
{ \
integer = integer << 8; \
integer += pdata[i]; \
} \
} while(0)
#define RETURN_INTEGER(integer) do { \
int i; \
if(datalen > 4) \
datalen = 4; \
for(i = 0; i < datalen; i++) \
{ \
pdata[datalen-i-1] = ((u8 *)&integer)[i]; \
} \
} while(0)
#undef GET_U8 /* undefine the "public" macro */
#define GET_U8(byte) do { \
*pdata = byte; \
*vartype = SyntaxInteger; \
datalen = 1; \
} while(0)
#define SET_U8(byte) do { \
byte = pdata[datalen-1]; \
} while(0)
#define GET_STRING(string) do { \
strcpy(pdata, string); \
*vartype = SyntaxOctetString; \
datalen = strlen(string); \
} while(0)
#define SET_STRING(string) do { \
memcpy(string, pdata, datalen); \
string[datalen] = 0; \
} while(0)
#define GET_OCTETSTRING(octets, count) do { \
memcpy(pdata, octets, count); \
*vartype = SyntaxOctetString; \
datalen = count; \
} while(0)
#define SET_OCTETSTRING(octets, count) do { \
memcpy(octets, pdata, count); \
} while(0)
#define GET_OPAQUE(octets, count) do { \
memcpy(pdata, octets, count); \
*vartype = SyntaxOpaque; \
datalen = count; \
} while(0)
#define SET_OPAQUE(octets, count) do { \
memcpy(octets, pdata, count); \
} while(0)
#define GET_IP(ip) do { \
memcpy(pdata, ip, 4); \
*vartype = SyntaxIpAddress; \
datalen = 4; \
} while(0)
#define SET_IP(ip) do { \
memcpy(ip, pdata, 4); \
} while(0)
#endif

View File

@@ -0,0 +1,330 @@
/*************************************************/
/*Title: snmp.h */
/*Descr: SNMP Agent(Head File) */
/*Author: Liang Hanxi */
/*Create: 2002-4-1 */
/*Modify: 2002-6-25 */
/*************************************************/
#ifndef _SNMP_HEAD_FILE
#define _SNMP_HEAD_FILE
#include "../../../debug/src/include/debug.h"
#include "../../../iptrans/src/include/iptrans.h"
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#define SNMP_DEBUG_MASK 0
#define SNMP_DEBUG_LOG 0
#define SNMP_VERSION {9, 0, 1}
#define SNMP_V1 0
#define SNMP_V2 1
#define COMMUNITY "public"
#define SNMP_ANY_COMMUNITY 0 //Should be set to 1 if snmp module is invoked in OMC
#define BIND_MAX 50
#define OID_MAXLEN 32
//#define FIX_OID {0x2b,0x06,0x01,0x04,0x01,0x8a,0x5d,0x01,0x01}
#define FIX_OID {1,3,6,1,4,1,1373,1,3}
//{iso(1).org(3).dod(6).internet(1).private(4).enterprises(1).interWAVE(1373).1.1}
#define FIX_OIDLEN 8
#define SNMP_OID {1,3,6,1,4,1,1373,1,3,2,3,3}
#define SNMP_OIDLEN 12
#define HEAP_SIZE 500*1024
#define SNMP_PORT 4957
#define SNMP_VAR_MAX 1600
//PDU
#define PDU_GET 0
#define PDU_GETNEXT 1
#define PDU_RSP 2
#define PDU_SET 3
#define PDU_GETBULK 5
#define PDU_INFORM 6
#define PDU_TRAP 7
#define PDU_REPORT 8
//variable type
#define NoSuchObject 0x80
#define NosuchInstance 0x81
#define EndOfMibView 0x82
//Debug
#define SNMPDB_IN 0x00
#define SNMPDB_OUT 0x01
#define SNMPDB_ERR 0x02
#define SNMPDB_NOREG 0x04
#define SNMPDB_VALUE 0x10
#define SNMPDB_ALARM 0x20
#define SNMPDB_MIB 0x40
#define SNMPDB_SYS 0x80
#ifndef _T_BYTE
#define _T_BYTE
typedef unsigned char BYTE;
#endif
#ifndef _T_BOOL
#define _T_BOOL
typedef unsigned char BOOL;
#endif
#ifndef _T_DWORD
#define _T_DWORD
typedef unsigned int DWORD;
#endif
#ifndef _T_WORD
#define _T_WORD
typedef unsigned short WORD;
#endif
// by simon at 23/9/25
//char SnmpVer[16];
typedef struct var_list{
DWORD oid[OID_MAXLEN];
BYTE oidlen;
BYTE vartype;
short msglen;
BYTE msg[SNMP_VAR_MAX];
}var_list;
typedef struct snmp_pdu //protocol data unit
{
BYTE pdu_type; //0-get request 2-get(set) respond 3-set request 7-trap
DWORD request_id;
char community[16];
BYTE var_num;
var_list var[BIND_MAX];
BYTE error_status; //0:no error 1:too big 2:nosuchname 3:badvalue 4:readonly 5:generr
BYTE error_index;
//add by yizane start
DWORD non_repeaters; //in getbulk replace error_status
DWORD max_repetitions; //in getbulk replace error_index
//add by yizane end
} snmp_pdu;
typedef struct snmp_addr
{
unsigned int remote_ip; //DWORD remote_ip;
unsigned int local_ip; //DWORD local_ip;
WORD remote_port;
WORD local_port;
BYTE broadcast;
} snmp_addr;
typedef struct snmp_msg
{
snmp_addr addr;
message_list msglist;
}snmp_msg;
typedef struct snmp_buf
{
snmp_msg msgout[256];
BYTE readsub, writesub;
}snmp_buf;
//snmp_buf snmp_outbuf;
typedef struct snmp_filter
{
BYTE flag;
BYTE type;
BYTE oidlen;
DWORD ip;
DWORD oid[OID_MAXLEN];
short local_port;
short remote_port;
}snmp_filter;
#define MAX_SNMP_RECORD 32
#define SNMP_SHM_KEY 0x60000001
#define SNMP_SHM_PERM 0666
typedef struct snmp_log_info
{
BYTE currentNo; /*from 0-MAX_SNMP_RECORD-1 */
char log_info[MAX_SNMP_RECORD][512];
}snmp_log_info;
/*---------------------------------------*/
typedef void (*trapcall)(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len,snmp_addr *addr);
typedef int (*setcall)(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len);
typedef int (*getcall)(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//add by yizane start
typedef int (*getnextcall)(BYTE in_oidlen, DWORD *in_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//add by yizane end
typedef void (*rspcall)(snmp_pdu *pdu,snmp_addr *addr);
typedef int (*snmpcall)(snmp_pdu *pdu,snmp_addr *addr);
typedef struct mib_nod{
DWORD id;
trapcall trap_proc;
setcall set_proc;
getcall get_proc;
//add by yizane start
getnextcall getnext_proc;
//add by yizane end
rspcall rsp_proc;
snmpcall snmp_proc;
struct mib_nod *pnext;
struct mib_nod *psub;
}mib_nod;
typedef struct snmp_csta{
DWORD timestamp;
DWORD trap_in;
DWORD trap_out;
DWORD set_in;
DWORD set_out;
DWORD get_in;
DWORD get_out;
//add by yizane start
DWORD getnext_in;
DWORD getnext_out;
DWORD getbulk_in;
DWORD getbulk_out;
//add by yizane end
DWORD rsp_in;
DWORD rsp_out;
DWORD fail_parse;
DWORD not_register;
DWORD fail_build;
}snmp_csta;
typedef struct snmp_mib{
BYTE sysid[2];
BYTE setpasswd[10];
BYTE getpasswd[10];
DWORD set_oid[64][20];
DWORD get_oid[64][20];
DWORD trap_oid[64][20];
DWORD rsp_oid[64][20];
}snmp_mib;
/********************************************************************/
/* Macros For ASN.1 Encoding */
/********************************************************************/
#define insert_length(buf,pos,len) {\
if(len<0)\
{\
snmp_debug(SNMPDB_ERR,"length should be larger than zero.");\
return 0;\
}\
else if(len<0x80)\
*(buf+pos--)=len;\
else if(len<0x100)\
{\
*(buf+pos--)=len;\
*(buf+pos--)=0x81;\
}\
else if(len<1600)\
{\
*(buf+pos--)=len & 0xFF;\
*(buf+pos--)=len >>8;\
*(buf+pos--)=0x82;\
}\
else\
{\
snmp_debug(SNMPDB_ERR,"Length(%d) exceed limit.varindex=%d",len,var_num);\
return 0;\
}\
}
#define enc_integer(buf,pos,val) {\
if(val<0)\
{\
snmp_debug(SNMPDB_ERR,"integer should larger than zero.val=%d",val);\
return -1;\
}\
else if(val<0x80)\
{\
*(buf+pos--)=(BYTE)val;\
*(buf+pos--)=1;\
}\
else if(val<0x8000)\
{\
*(buf+pos--)=val & 0xFF;\
*(buf+pos--)=val>>8;\
*(buf+pos--)=2;\
}\
else if(val<0x800000)\
{\
*(buf+pos--)= val & 0xFF;\
*(buf+pos--)=(val & 0xFFFF)>>8;\
*(buf+pos--)=val>>16;\
*(buf+pos--)=3;\
}\
else if(val<0x80000000)\
{\
*(buf+pos--)= val & 0xFF;\
*(buf+pos--)=(val & 0xFFFF)>>8;\
*(buf+pos--)=(val & 0xFFFFFF)>>16;\
*(buf+pos--)=val>>24;\
*(buf+pos--)=4;\
}\
else if(val<0xFFFFFFFF)\
{\
*(buf+pos--)= val & 0xFF;\
*(buf+pos--)=(val & 0xFFFF)>>8;\
*(buf+pos--)=(val & 0xFFFFFF)>>16;\
*(buf+pos--)=val>>24;\
*(buf+pos--)=0;\
*(buf+pos--)=5;\
}\
else\
{\
snmp_debug(SNMPDB_ERR,"Integer's value exceed limit.val=%d",val);\
return -1;\
}\
}
void snmp_init(WORD nport);
void snmpmib_init();
void snmp_timer();
int snmp_send(snmp_pdu *msgbuf,snmp_addr *addr);
int snmp_plat_send(snmp_pdu *msgbuf,snmp_addr *addr);
int snmp_receive(snmp_pdu *msgbuf,snmp_addr *addr);
int get_response(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
int set_response(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD datalen);
//add by yizane start
int getnext_response(BYTE pr_oidlen, DWORD *pre_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//add by yizane end
void inquire_trapmsg(trapcall trap_proc);
void inquire_setmsg(BYTE oidlen,DWORD *oid,setcall set_proc);
void inquire_getmsg(BYTE oidlen,DWORD *oid,getcall get_proc);
void inquire_rspmsg(BYTE oidlen,DWORD *oid,rspcall rsp_proc);
void inquire_snmpmsg(BYTE oidlen,DWORD *oid,snmpcall snmp_proc);
//add by yizane start
void inquire_getnextmsg(BYTE oidlen,DWORD *oid,getnextcall getnext_proc);
//add by yizane end
void rob_trapmsg(BYTE oidlen,DWORD *oid,trapcall trap_proc);
BYTE encode_integer(BYTE *buf,DWORD val);
DWORD decode_integer(BYTE *buf,BYTE len);
void snmp_log(char *info);
void snmp_debug(DWORD mask, const char *fmt,...);
void snmp_showbuf(DWORD mask, BYTE *buf, int len);
void snmp_logmsg(DWORD mask, snmp_pdu *pdu, snmp_addr *addr);
void snmp_debug_setup();
void snmp_debug_timer();
void snmp_debug_csta(BYTE npage,int instance);
void snmp_debug_hb(BYTE npage);
void snmp_init_serial_number(BYTE *Serial);
void snmp_set_timenosync();//call of this function will tell snmp module do not sync the omc time
#include "table.h"
/********************************************************************/
#endif

View File

@@ -0,0 +1,61 @@
/*************************************************/
/*Title: sysctrl.h */
/*Descr: */
/*Author: Liang Hanxi */
/*Create: 2002-4-5 */
/*Modify: 2002-6-25 */
/*************************************************/
#ifndef __SYSCTRL__H
#define __SYSCTRL__H
#include "time.h"
#include <sys/vfs.h>
#include <errno.h>
#include <mntent.h>
#include <sys/sysinfo.h>
#include "heartbeat.h"
#define SYS_T_report 50*60*1 //1min
/* fj added 2018-01-03 omcMain <20>ϱ<EFBFBD><CFB1><EFBFBD><E8B1B8>Ϣ */
#pragma pack(1)
#define MAX_PARTITION_NUM 16
typedef struct partition
{
BYTE num; // ·<><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BYTE pos; // ·<><C2B7>ֵ<EFBFBD><D6B5>
// char mountpoint_path[16];
DWORD used_space;
DWORD total_space;
}partition;
typedef struct hb_sysinfo3{
DWORD up_time;
BYTE idle_cpu_rate;
DWORD ram_total_used;
DWORD ram_total;
DWORD ram_swap_used;
DWORD ram_swap_total;
DWORD ram_buffers;
DWORD ram_cached;
BYTE pt_count;
partition pt[MAX_PARTITION_NUM];
}hb_sysinfo3;
#pragma pack()
typedef struct mnt_list
{
char mnt_fsname[50];
char mnt_dir[50];
DWORD total;
DWORD free;
DWORD limit;
}mnt_list;
void sysctrl_init();
void system_report();
#endif

View File

@@ -0,0 +1,42 @@
#ifndef _SNMP_TABLE_H
#define _SNMP_TABLE_H
#define MAX_COLUMNAR_OF_TABLE 48
typedef struct table_index {
u8 len;
u32 oid[32];
} table_index;
/* Get an exist instance from the inclusive one pointed to by address 'instance' */
typedef int (*table_get_instance)(u32 *instance);
typedef int (*table_get_resp)(u32 column, u32 *instance, u8 *pdata, u8 *vartype);
typedef int (*table_set_resp)(u32 column, u32 *instance, u8 *pdata, u16 datalen);
struct snmp_table_profile {
char name[32];
table_index index; /* oid of table with entry exclusive */
u32 column[MAX_COLUMNAR_OF_TABLE]; /* define all columns of a table, use '0' indicating the end */
u8 level; /* level of instance in order to identify a row */
table_get_instance get_instance;
table_get_resp get_resp;
table_set_resp set_resp;
};
struct snmp_register {
char name[32];
u8 prefix_len; /* Module prefix len */
u32 prefix_oid[32]; /* Module prefix oid */
u8 num_of_table; /* how many tables being registered, such tables is pointed to by address 'table' */
struct snmp_table_profile *table;
/* In case no table is matched, default callback function is invoked if it is not NULL. Hence,
you can implement get/set in much the same way before, i.e. traveling all branches of MIB */
getcall default_get_resp;
setcall default_set_resp;
};
extern int register_snmp_table(struct snmp_register *reg);
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,171 @@
/********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: snmp_mib.h
Description: header file of snmp mib parser
Version: v9.0.0
Author: Roy Jiang
Create Date: 2007-6-9
History:
2007-6-9 v9.0.0 Create
BEWARE: Portions of this file are subject to the copyrights of
open-source project Net-SNMP.See the Net-SNMP's COPYING file
for more details
*********************************************************************/
#ifndef __WXC2_SNMP_MIB_H_
#define __WXC2_SNMP_MIB_H_
#define MAXLABEL 64 /* maximum characters in a label */
#define MAXTOKEN 128 /* maximum characters in a token */
#define MAXQUOTESTR 20480 /* maximum characters in a quoted string */
struct variable_list;
/*
* A linked list of tag-value pairs for enumerated integers.
*/
struct enum_list {
struct enum_list *next;
int value;
char *label;
};
/*
* A linked list of ranges
*/
struct range_list {
struct range_list *next;
int low, high;
};
/*
* A linked list of indexes
*/
struct index_list {
struct index_list *next;
char *ilabel;
char isimplied;
};
/*
* A linked list of varbinds
*/
struct varbind_list {
struct varbind_list *next;
char *vblabel;
};
/*
* A tree in the format of the tree structure of the MIB.
*/
struct tree {
struct tree *child_list; /* list of children of this node */
struct tree *next_peer; /* Next node in list of peers */
struct tree *next; /* Next node in hashed list of names */
struct tree *parent;
char *label; /* This node's textual name */
int subid; /* This node's integer subidentifier */
int modid; /* The module containing this node */
int number_modules;
int *module_list; /* To handle multiple modules */
int tc_index; /* index into tclist (-1 if NA) */
int type; /* This node's object type */
int access; /* This nodes access */
int status; /* This nodes status */
struct enum_list *enums; /* (optional) list of enumerated integers */
struct range_list *ranges;
struct index_list *indexes;
char *augments;
struct varbind_list *varbinds;
char *hint;
char *units;
char *description; /* description (a quoted string) */
char *reference; /* references (a quoted string) */
int reported; /* 1=report started in print_subtree... */
char *defaultValue;
int oid[32];
int oid_len;
};
/*
* Information held about each MIB module
*/
struct module_import {
char *label; /* The descriptor being imported */
int modid; /* The module imported from */
};
struct module {
char *name; /* This module's name */
char *file; /* The file containing the module */
struct module_import *imports; /* List of descriptors being imported */
int no_imports; /* The number of such import descriptors */
/*
* -1 implies the module hasn't been read in yet
*/
int modid; /* The index number of this module */
struct module *next; /* Linked list pointer */
};
struct module_compatability {
const char *old_module;
const char *new_module;
const char *tag; /* NULL implies unconditional replacement,
* otherwise node identifier or prefix */
size_t tag_len; /* 0 implies exact match (or unconditional) */
struct module_compatability *next; /* linked list */
};
/*
* non-aggregate types for tree end nodes
*/
#define TYPE_OTHER 0
#define TYPE_OBJID 1
#define TYPE_OCTETSTR 2
#define TYPE_INTEGER 3
#define TYPE_NETADDR 4
#define TYPE_IPADDR 5
#define TYPE_COUNTER 6
#define TYPE_GAUGE 7
#define TYPE_TIMETICKS 8
#define TYPE_OPAQUE 9
#define TYPE_NULL 10
#define TYPE_COUNTER64 11
#define TYPE_BITSTRING 12
#define TYPE_NSAPADDRESS 13
#define TYPE_UINTEGER 14
#define TYPE_UNSIGNED32 15
#define TYPE_INTEGER32 16
#define TYPE_SIMPLE_LAST 16
#define TYPE_TRAPTYPE 20
#define TYPE_NOTIFTYPE 21
#define TYPE_OBJGROUP 22
#define TYPE_NOTIFGROUP 23
#define TYPE_MODID 24
#define TYPE_AGENTCAP 25
#define TYPE_MODCOMP 26
#define TYPE_OBJIDENTITY 27
#define MIB_ACCESS_READONLY 18
#define MIB_ACCESS_READWRITE 19
#define MIB_ACCESS_WRITEONLY 20
#define MIB_ACCESS_NOACCESS 21
#define MIB_ACCESS_NOTIFY 67
#define MIB_ACCESS_CREATE 48
#define MIB_STATUS_MANDATORY 23
#define MIB_STATUS_OPTIONAL 24
#define MIB_STATUS_OBSOLETE 25
#define MIB_STATUS_DEPRECATED 39
#define MIB_STATUS_CURRENT 57
#define ANON "anonymous#"
#define ANON_LEN strlen(ANON)
#endif

View File

@@ -0,0 +1,122 @@
/***********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: snmp_mib_api.c
Description: API for SNMP MIB parser
Version: v9.0.0
Author: Roy Jiang
Create Date: 2007-6-19
History:
2007-6-9 v9.0.0 Create
***********************************************************************/
#include <stdio.h>
#include <sys/stat.h>
#include "snmp_mib.h"
#include "snmp_mib_api.h"
const char *mib_dir = "./mib";
const char *index_file = "index";
extern struct tree *tree_head;
extern struct module *module_head;
extern void new_module(const char *name, const char *file);
extern struct tree *read_module(const char *name);
extern int get_token(FILE * fp, char *token, int maxtlen);
extern void print_subtree(FILE * f, struct tree *tree, int count);
extern struct tree *find_tree_node(const char *name, int modid);
extern void free_tree(struct tree *Tree);
//return 0 on success
int init_mib()
{
//open dir and check mib files in the dir
{
FILE *ip, *fp;
char tmpstr[300];
int count = 0;
char token[MAXTOKEN];
char newline;
struct stat dir_stat, idx_stat;
char tmpstr1[300];
printf("Scanning directory %s\n", mib_dir);
snprintf(token, sizeof(token), "%s/%s", mib_dir, index_file);
token[ sizeof(token)-1 ] = 0;
if (stat(token, &idx_stat) == 0 && stat(mib_dir, &dir_stat) == 0) {
if ((ip = fopen(token, "r")) != NULL) {
while (fscanf(ip, "%256s%c", tmpstr, &newline) == 2)
{
/*
* If an overflow of the token or tmpstr buffers has been
* found log a message and break out of the while loop,
* thus the rest of the file tokens will be ignored.
*/
if (newline != '\n') {
printf("add_mibdir: strings scanned in from %s/%s " \
"are too large. count = %d\n ", mib_dir,
".index", count);
break;
}
snprintf(tmpstr1, sizeof(tmpstr1), "%s/%s", mib_dir, tmpstr);
tmpstr1[ sizeof(tmpstr1)-1 ] = 0;
fp = fopen(tmpstr1, "r");
if (fp == NULL) {
printf("Can not open mib file: %s\n", tmpstr1);
continue;
}
get_token(fp, token, MAXTOKEN);
fclose(fp);
new_module(token, tmpstr1);
count++;
}
fclose(ip);
}
else
printf("Can't read index\n");
}
else
printf("No index\n");
}
//read all mibs
{
struct module *mp;
for (mp = module_head; mp; mp = mp->next)
if (mp->no_imports == -1)
read_module(mp->name);
}
#ifdef _TEST_
{
//for debug
// print_subtree(stdout, tree_head, 0);
}
#endif
return 0;
}
struct tree *getLGCroot()
{
return find_tree_node("lgcNS", -1);
}
void close_mib()
{
free_tree(tree_head);
return;
}
//test----------------------------------------------------------------------------
#ifdef _TEST_
int main(int argc, char *argv[])
{
init_mib();
return 0;
}
#endif
//end of test---------------------------------------------------------------------

View File

@@ -0,0 +1,49 @@
/***********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: snmp_mib.h
Description: header file of snmp mib parser
Version: v9.0.0
Author: Roy Jiang
Create Date: 2007-6-9
History:
2007-6-9 v9.0.0 Create
***********************************************************************/
#ifndef _WXC2_SNMP_MIB_API_H_
#define _WXC2_SNMP_MIB_API_H_
#include "snmp_mib.h"
/*
*Purpose: Init mib parser, read all mib files listed in ./mib/index,
* you should config which mib file you want to parse in the file./mib/index
*Input: None
*Output: None
*/
extern int init_mib();
/*
*Purpose: get the LGC root node of mib tree
*Input: None
*Output: LGC root tree node
*/
extern struct tree *getLGCroot();
/*
*Purpose: Close mib parser and release all resources used in parsing mib files
*Input: None
*Output: None
*/
extern void close_mib();
/*
*Purpose: Get tree node from oid
*Input: const int oid: oid of the tree node to get
int oidlen: the length of the oid
struct tree *subtree: tree node from which the search start
*Output: tree node
*/
extern struct tree *get_tree(const int *oid, int oidlen, struct tree *subtree);
#endif

2022
plat/snmp/src/snmp.c Normal file

File diff suppressed because it is too large Load Diff

496
plat/snmp/src/snmpdebug.c Normal file
View File

@@ -0,0 +1,496 @@
/**************************************************/
/*Title: snmpdebug.c */
/*Descr: Debug Module of SNMP Agent */
/*Author: Liang Hanxi */
/*Create: 2002-4-1 */
/*Modify: */
/**************************************************/
#include "./include/heartbeat.h"
static char debug_buf[4096];
static char asc_in[256];
static DWORD title_id[20]={1,3,6,1,4,1,1373,1,3,2,3,1,2,13,8,1};
//private function
BYTE snmpDebugFull = 0;
static snmp_filter SnmpFilter[8];
static BYTE SnmpFilterIndex = 0;
static DWORD debug_mask = SNMP_DEBUG_MASK;
extern snmp_csta SnmpCsta[96];
extern heartbeat_shm *HbShm;
static char Type_Name[9][16] = {"Get", "GetNext"
, "Response", "Set", "Trap-V1", "GetBulk", "Inform", "Trap-V2", "Report"};
static char help_info[] = "Commands provided now:\r\n\
help\r\n\
log all Output all debug information\r\n\
log none Turn off all information\r\n\
log error on/off Show error information\r\n\
log noreg on/off Alarm if oid is not registered\r\n\
log value on/off Show value party of snmp message\r\n\
log alarm on/off Show alarm information of platform\r\n\
log sys on/off Show system information\r\n\
log type cond1 (and cond2...) Show message due to specified condition\r\n\
type should be snmp/trap/set/get/rsp\r\n\
One or more conditions could be taken:\r\n\
oid=x.x../ip=xx/local port=xx/remote port=xx\r\n\
For example:log trap oid=1.3.6.1.4.1373 and ip=172.18.98.1\r\n\
list register List all registered oid\r\n\
list status List system status\
";
int snmp_filter_msg(snmp_pdu *pMsg, snmp_addr *pAddr);
extern int Str2Oid(DWORD *pOid, char *pStr, BYTE maxLen);
/* by simon at 23/9/25 */
extern char SnmpVer[16];
void snmp_debug_setup()
{
DWORD asc_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,4};
DWORD ascin_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,3};
DWORD status_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,1};
DWORD name_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,2};
static BYTE debug_status=1;
debug_set_response(15,asc_id,debug_buf,1);
debug_set_response(15,ascin_id,asc_in,1);
debug_set_response(15,status_id,&debug_status,1);
debug_set_response(15,name_id, SnmpVer, strlen(SnmpVer));
}
void snmp_log(char *info)
{
if(strlen(debug_buf) + strlen(info) > 4096)
{
snmpDebugFull = 1;
return;
}
strncat(debug_buf, info, 4094);
strcat(debug_buf, "\r\n");
return;
}
int snmp_filter_msg(snmp_pdu *pMsg, snmp_addr *pAddr)
{
int i;
for(i = 0; i < SnmpFilterIndex % 9; i++)
{
if(SnmpFilter[i].type != pMsg->pdu_type && pMsg->pdu_type != 0xFF)
continue;
if(SnmpFilter[i].flag & 0x01)
{
if(SnmpFilter[i].ip != pAddr->remote_ip)
continue;
}
if(SnmpFilter[i].flag & 0x02)
{
if(memcmp(SnmpFilter[i].oid, pMsg->var[0].oid, SnmpFilter[i].oidlen * 4) != 0)
continue;
}
if(SnmpFilter[i].flag & 0x04)
{
if(SnmpFilter[i].local_port != pAddr->local_port)
continue;
}
if(SnmpFilter[i].flag & 0x08)
{
if(SnmpFilter[i].remote_port != pAddr->remote_port)
continue;
}
return 1;
}
return 0;
}
void snmp_debug(DWORD mask, const char *fmt, ...)
{
va_list ap;
char buf[1024];
if((mask & debug_mask) == 0 || snmpDebugFull)
return;
if(mask & SNMPDB_ERR)
strcat(debug_buf, "\33[31m");
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
strcat(buf,"\33[37m");
snmp_log(buf);
}
void snmp_showbuf(DWORD mask, BYTE *buf, int len)
{
int i = 0, j = 0, linemax = 24;
char info[1024];
if((debug_mask & SNMPDB_VALUE) == 0 || (debug_mask & mask) == 0 || snmpDebugFull)
return;
if(len >= 256)
len = 256;
while(i * 24 < len)
{
if((linemax = len - 24 * i) > 24)
linemax = 24;
for(j = 0; j < linemax; j ++)
{
sprintf(info + 3 * j, "%02X ", buf[24 * i + j]);
}
snmp_log(info);
i ++;
}
}
int oid2str(char *pStr, DWORD *oid, BYTE oidlen)
{
int i;
char tmpstr[16];
pStr[0] = 0;
if(oidlen > OID_MAXLEN) oidlen = OID_MAXLEN;
for(i = 0; i < oidlen - 1; i++)
{
sprintf(tmpstr, "%ld.", oid[i]);
strcat(pStr, tmpstr);
}
sprintf(tmpstr, "%ld", oid[oidlen - 1]);
strcat(pStr, tmpstr);
return 1;
}
void snmp_logvalue(BYTE *msgbuf, short msglen)
{
int len, i = 0, j, linemax;
char info[1024];
len = msglen % 256;
while(i * 24 < len)
{
if((linemax = len - 24 * i) > 24)
linemax = 24;
for(j = 0; j < linemax; j ++)
{
sprintf(info + 3 * j, "%02X ", msgbuf[24 * i + j]);
}
snmp_log(info);
i ++;
}
}
void snmp_logmsg(DWORD mask, snmp_pdu *pdu, snmp_addr *addr)
{
struct in_addr inaddr;
int i = 0;
char info[1024], oidstr[64], desc[64] = "\0";
if((debug_mask & mask) == 0 && snmp_filter_msg(pdu, addr) == 0)
return;
if(snmpDebugFull)
return;
if(mask & SNMPDB_ERR)
strcat(debug_buf, "\33[31m");
inaddr.s_addr = addr->remote_ip;
if(mask & SNMPDB_IN)
sprintf(desc, "\r\nlocal:%d <== %s:%d ", addr->local_port, inet_ntoa(inaddr), addr->remote_port);
else if(mask & SNMPDB_OUT)
sprintf(desc, "\r\nlocal:%d ==> %s:%d ", addr->local_port, inet_ntoa(inaddr), addr->remote_port);
else
sprintf(desc, "\r\nlocal:%d <=> %s:%d ", addr->local_port, inet_ntoa(inaddr), addr->remote_port);
sprintf(info, "%sType=%s, RID=%ld, Error=%d, Bind=%d\33[37m", desc, Type_Name[pdu->pdu_type], pdu->request_id
, pdu->error_status, pdu->var_num);
snmp_log(info);
for(i = 0; i < pdu->var_num % 16; i++)
{
oid2str(oidstr, pdu->var[i].oid, pdu->var[i].oidlen);
sprintf(info, "oid[%d]=%s, vartype=%02x, msglen=%d", i, oidstr, pdu->var[i].vartype, pdu->var[i].msglen);
snmp_log(info);
if((debug_mask & SNMPDB_VALUE) == 0)
return;
snmp_logvalue(pdu->var[i].msg, pdu->var[i].msglen);
}
}
static int get_word(char *pWord, char *pInStr, BYTE max)
{
char *pStart = pInStr, *pCh = NULL;
while(*pStart == ' ' || *pStart == '\t')
pStart ++;
strncpy(pWord, pStart, max);
pCh = strchr(pWord, ' ');
if(pCh != NULL)
*pCh = 0;
return 1;
}
static int parsecond(char *cmdstr, BYTE type)
{
char *pKey = cmdstr, *pEQ, info[256], tmpStr[64], valStr[32];
if(SnmpFilterIndex >= 8)
{
snmp_log("only 8 pieces of condition can be set");
return 0;
}
sprintf(info, "type = 0x%02X", type);
SnmpFilter[SnmpFilterIndex].type = type;
SnmpFilter[SnmpFilterIndex].flag = 0;
if((pKey = strstr(cmdstr, "ip")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
SnmpFilter[SnmpFilterIndex].ip = inet_addr(valStr);
sprintf(tmpStr, " and ip = %s", valStr);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].flag |= 0x01;
}
if((pKey = strstr(cmdstr, "oid")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
if(SnmpFilter[SnmpFilterIndex].flag > 0) strcat(info, " and ");
sprintf(tmpStr, " and oid = %s", valStr);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].oidlen = Str2Oid(SnmpFilter[SnmpFilterIndex].oid, valStr, OID_MAXLEN) % OID_MAXLEN;
SnmpFilter[SnmpFilterIndex].flag |= 0x02;
}
if((pKey = strstr(cmdstr, "local port")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
SnmpFilter[SnmpFilterIndex].local_port = atoi(valStr);
if(SnmpFilter[SnmpFilterIndex].flag > 0) strcat(info, " and ");
sprintf(tmpStr, " and local port = %d", SnmpFilter[SnmpFilterIndex].local_port);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].flag |= 0x04;
}
if((pKey = strstr(cmdstr, "remote port")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
SnmpFilter[SnmpFilterIndex].remote_port = atoi(valStr);
if(SnmpFilter[SnmpFilterIndex].flag > 0) strcat(info, " and ");
sprintf(tmpStr, " and remote port = %d", SnmpFilter[SnmpFilterIndex].remote_port);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].flag |= 0x08;
}
SnmpFilterIndex ++;
snmp_log(info);
snmp_log("command OK");
return 1;
}
void snmp_debug_timer()
{
BYTE npage, nline;
char *cmdstr = asc_in + 1;
if(strlen(asc_in) <= 1)
return;
debug_buf[0] = '\0';
npage = asc_in[0] - 1;
if(npage > 0)
{
nline = atoi(asc_in + 1);
switch(npage)
{
case 1: //csta
snmp_debug_csta(1,nline);
break;
}
asc_in[0]='\0';
return;
}
asc_in[0]='\0';
if(strstr(cmdstr, "help") != NULL || strstr(cmdstr, "?") != NULL)
{
snmp_log(help_info);
return;
}
else if(strstr(cmdstr, "log all") != NULL)
{
SnmpFilterIndex = 0;
debug_mask = 0xFFFFFFFF;
}
else if(strstr(cmdstr, "log none") != NULL)
{
SnmpFilterIndex = 0;
debug_mask = 0;
}
else if(strstr(cmdstr, "log error on") != NULL)
debug_mask |= SNMPDB_ERR;
else if(strstr(cmdstr, "log error off") != NULL)
debug_mask &= (~SNMPDB_ERR);
else if(strstr(cmdstr, "log onreg on") != NULL)
debug_mask |= SNMPDB_NOREG;
else if(strstr(cmdstr, "log onreg off") != NULL)
debug_mask &= (~SNMPDB_NOREG);
else if(strstr(cmdstr, "log value on") != NULL)
debug_mask |= SNMPDB_VALUE;
else if(strstr(cmdstr, "log value off") != NULL)
debug_mask &= (~SNMPDB_VALUE);
else if(strstr(cmdstr, "log alarm on") != NULL)
debug_mask |= SNMPDB_ALARM;
else if(strstr(cmdstr, "log alarm off") != NULL)
debug_mask &= (~SNMPDB_ALARM);
else if(strstr(cmdstr, "log mib on") != NULL)
debug_mask |= SNMPDB_MIB;
else if(strstr(cmdstr, "log mib off") != NULL)
debug_mask &= (~SNMPDB_MIB);
else if(strstr(cmdstr, "log sys on") != NULL)
debug_mask |= SNMPDB_SYS;
else if(strstr(cmdstr, "log sys off") != NULL)
debug_mask &= (~SNMPDB_SYS);
else if(strstr(cmdstr, "log snmp") != NULL)
{
parsecond(cmdstr + 8, 0xFF);
return;
}
else if(strstr(cmdstr, "log get") != NULL)
{
parsecond(cmdstr + 7, 0);
return;
}
else if(strstr(cmdstr, "log set") != NULL)
{
parsecond(cmdstr + 7, 3);
return;
}
else if(strstr(cmdstr, "log rsp") != NULL)
{
parsecond(cmdstr + 7, 2);
return;
}
else if(strstr(cmdstr, "log trap") != NULL)
{
parsecond(cmdstr + 8, 7);
return;
}
else
{
snmp_log("Invalid command/parameter(s).");
return;
}
snmp_log("Command OK");
return;
}
void snmp_debug_info(BYTE npage, BYTE nline, BYTE *pt, BYTE len)
{
DWORD oid[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,5,2,2};
oid[14] = 5 + npage;
oid[15] = 2 + nline;
debug_set_response(17, oid, pt, len);
}
/*-----------------------------------------------*/
/*-------------------CSTA------------------------*/
/*-----------------------------------------------*/
void snmp_debug_csta(BYTE npage,int instance)
{
char title[256];
snmp_csta *pcsta;
instance=instance%96;
pcsta=&SnmpCsta[instance];
sprintf(title,"\tSNMP-%s\tPage %02d\t\tMIB-CSTA(ID:1 Instance:%d)\r\n\r\n"\
"trap in\r\n"\
"trap out\r\n"\
"set in\r\n"\
"set out\r\n"\
"get in\r\n"\
"get out\r\n"\
"rsp in\r\n"\
"rsp out\r\n"\
"fail to parse\r\n"\
"OID Not registered\r\n"\
"fail to build\r\n", SnmpVer, npage, instance);
title_id[14]=5+npage;
debug_set_response(16,title_id,title,strlen(title));
snmp_debug_info(npage,0,(BYTE *)&pcsta->trap_in,4);
snmp_debug_info(npage,1,(BYTE *)&pcsta->trap_out,4);
snmp_debug_info(npage,2,(BYTE *)&pcsta->set_in,4);
snmp_debug_info(npage,3,(BYTE *)&pcsta->set_out,4);
snmp_debug_info(npage,4,(BYTE *)&pcsta->get_in,4);
snmp_debug_info(npage,5,(BYTE *)&pcsta->get_out,4);
snmp_debug_info(npage,6,(BYTE *)&pcsta->rsp_in,4);
snmp_debug_info(npage,7,(BYTE *)&pcsta->rsp_out,4);
snmp_debug_info(npage,8,(BYTE *)&pcsta->fail_parse,4);
snmp_debug_info(npage,9,(BYTE *)&pcsta->not_register,4);
snmp_debug_info(npage,10,(BYTE *)&pcsta->fail_build,4);
}
void snmp_debug_hb(BYTE npage)
{
char title[512];
sprintf(title,"\tSNMP-%s\tPage %02d\t\tHeartBeat\r\n"\
"\r\n"\
"subsys id\r\n"\
"LED\r\n"\
"Component 0\r\n"\
"\r\n"\
"Component 1\r\n"\
"\r\n"\
"Component 2\r\n"\
"\r\n"\
"Component 3\r\n"\
"\r\n"\
"Component 4\r\n"\
"\r\n"\
"Component 5\r\n"\
"\r\n"\
"Component 6\r\n"\
"\r\n"\
"alarm num\r\n"\
"HB message\r\n\r\n", SnmpVer, npage);
title_id[14]=5+npage;
debug_set_response(16,title_id,title,strlen(title));
snmp_debug_info(npage,0,(BYTE *)&HbShm->sys_id,2);
snmp_debug_info(npage,1,(BYTE *)HbShm->led,LED_NUM);
snmp_debug_info(npage,2,(BYTE *)&HbShm->component[0],20);
snmp_debug_info(npage,3,(BYTE *)&HbShm->component[0]+20,20);
snmp_debug_info(npage,4,(BYTE *)&HbShm->component[1],20);
snmp_debug_info(npage,5,(BYTE *)&HbShm->component[1]+20,20);
snmp_debug_info(npage,6,(BYTE *)&HbShm->component[2],20);
snmp_debug_info(npage,7,(BYTE *)&HbShm->component[2]+20,20);
snmp_debug_info(npage,8,(BYTE *)&HbShm->component[3],20);
snmp_debug_info(npage,9,(BYTE *)&HbShm->component[3]+20,20);
snmp_debug_info(npage,10,(BYTE *)&HbShm->component[4],20);
snmp_debug_info(npage,11,(BYTE *)&HbShm->component[4]+20,20);
snmp_debug_info(npage,12,(BYTE *)&HbShm->component[5],20);
snmp_debug_info(npage,13,(BYTE *)&HbShm->component[5]+20,20);
snmp_debug_info(npage,14,(BYTE *)&HbShm->component[6],20);
snmp_debug_info(npage,15,(BYTE *)&HbShm->component[6]+20,20);
snmp_debug_info(npage,16,(BYTE *)&HbShm->alarm_num,1);
snmp_debug_info(npage,17,(BYTE *)&HbShm->msg,20);
snmp_debug_info(npage,18,(BYTE *)&HbShm->msg+20,20);
snmp_debug_info(npage,19,(BYTE *)&HbShm->msg+40,20);
snmp_debug_info(npage,20,(BYTE *)&HbShm->msg+60,20);
snmp_debug_info(npage,21,(BYTE *)&HbShm->msg+80,20);
}

270
plat/snmp/src/snmpmib.c Normal file
View File

@@ -0,0 +1,270 @@
/**************************************************/
/*Title: snmpdebug.c */
/*Descr: Debug Module of SNMP Agent */
/*Author: Liang Hanxi */
/*Create: 2002-4-1 */
/*Modify: */
/**************************************************/
#include "./include/heartbeat.h"
static unsigned long l_starttime;
extern snmp_csta SnmpCsta[96];
extern int snmp_ins;
extern heartbeat_shm *HbShm;
int snmp_setmib(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD datalen);
int snmp_getmib(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
int snmp_getsys(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
void time_sync(BYTE oidlen,DWORD *oid,BYTE *pdata,WORD len,snmp_addr *addr);
static void submit_snmpcsta(BYTE *pdword,BYTE *pbyte,int len);
/* by simon at 23/9/25 */
extern char SnmpVer[16];
void snmpmib_init()
{
DWORD oid[OID_MAXLEN]=SNMP_OID;
DWORD sysoid[8]={1,3,6,1,2,1,1};
l_starttime=time(NULL);
inquire_getmsg(SNMP_OIDLEN,oid,snmp_getmib);
inquire_setmsg(SNMP_OIDLEN,oid,snmp_setmib);
inquire_getmsg(7,sysoid,snmp_getsys);
inquire_trapmsg(time_sync);
}
int snmp_setmib(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD datalen)
{
return -1;
switch(oid[SNMP_OIDLEN])
{
case 1: //Measure
return -1; //read only
case 2: //Configuration
switch(oid[SNMP_OIDLEN+1])
{
case 1: //SNMP Parameter
switch(oid[SNMP_OIDLEN+2])
{
case 1: //Port
return -1; //not allow
case 2: //password
return -1; //not provide now
}
break;
case 2:
break;
case 3:
break;
}
break;
case 3:
break;
}
}
int snmp_getmib(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
int instance;
snmp_csta *pcsta;
switch(oid[SNMP_OIDLEN])
{
case 1: //csta
if(oidlen!=SNMP_OIDLEN+2)
return -1;
instance=oid[SNMP_OIDLEN+1];
if(instance==snmp_ins || instance>95)
{
*vartype=NosuchInstance;
snmp_debug(SNMPDB_MIB,"csta message not ready.instance=%d",instance);
return -1;
}
pcsta=&SnmpCsta[instance];
if(pcsta->timestamp ==0)
{
snmp_debug(SNMPDB_MIB,"csta message is expired");
*vartype=5;
return 0;
}
*vartype=4;
snmp_debug(SNMPDB_MIB, "Respond csta message");
submit_snmpcsta((BYTE *)&pcsta->timestamp,pdata,sizeof(snmp_csta));
//memcpy(pdata,(BYTE *)&pcsta->timestamp,sizeof(snmp_csta));
return sizeof(snmp_csta);
default:
return -1;
}
return -1;
}
int snmp_getsys(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
unsigned long l_time;
char contact[50]="InterWAVE";
char sysname[50];
switch(oid[oidlen - 1])
{
case 1:
*vartype = 4;
memcpy(pdata, SnmpVer, strlen(SnmpVer));
return strlen(SnmpVer);
case 3:
l_time=time(NULL);
*vartype=0x43;
return encode_integer(pdata,(l_time-l_starttime)*100);
case 4:
*vartype=4;
memcpy(pdata,contact,strlen(contact));
return strlen(contact);
case 5:
*vartype=4;
sprintf(sysname,"SNMP Agent %d.%d",HbShm->sys_id,HbShm->subsys_id);
memcpy(pdata,sysname,strlen(sysname));
return strlen(sysname);
}
return -1;
}
#define SNMP_OMC_HB_OID {1,3,6,1,4,1,1373,2,4,1}
#define SNMP_OMC_HB_OIDLEN 10
#define SNMP_SERNUM_LEN 4
unsigned long long Bcd2Ull(BYTE *pBcd, int BcdLen);
extern unsigned long long SNMPSerialNumber;
extern BYTE snmp_synctime_flag;
void time_sync(BYTE oidlen,DWORD *oid,BYTE *pdata,WORD len,snmp_addr *addr)
{
//DWORD ObjID[20]=FLATFORM_HB_OID;
DWORD OMCHBObjID[20] = SNMP_OMC_HB_OID;
heartbeat_msg *pmsg=(heartbeat_msg *)pdata;
struct timeval tv;
struct timezone tz;
int l_time,l_temp;
static int master_host=0,master_timeout;
char oidstring[256];
char tmpstr[64];
oidstring[0]=0;
int i;
if (!snmp_synctime_flag)
return;
if(memcmp(&oid[7], &OMCHBObjID[7], (SNMP_OMC_HB_OIDLEN - 7) * 4) == 0)//omc heartbeat
{//automatic add omc ip into ip list,
//if receive omc heartbeat and the omc serial number is equial to the local serial number,
//then the platform will send heartbeat if received omc heartbeat
if ((SNMPSerialNumber==0) ||//support old version, do not have serial number
(Bcd2Ull(&pdata[39],SNMP_SERNUM_LEN)==SNMPSerialNumber) )
{}
else
return;
}
else// if it is not omc heartbeat then return
return;
if(pmsg->subsys_id ==HbShm->subsys_id && pmsg->sys_id ==HbShm->sys_id)
return;
/*
if(memcmp(&oid[9],&ObjID[9],(FLATFORM_HB_OIDLEN-9)*4)!=0)
{
return;
}
*/
/*
if(pmsg->sys_id!=MasterSysID || pmsg->subsys_id!=MasterSubSysID)
return;
*/
if(SNMPSerialNumber!=0){//New version
if(pdata[26]!=5&&pdata[26]!=7)//not the master omc
return;
}else{//Old version
if(pdata[21]!=5&&pdata[21]!=7)//not the master omc
return;
}
memcpy((BYTE *)&l_temp,pmsg->timestamp,4);
l_time=htonl(l_temp); //timestamp in heartbeat
gettimeofday(&tv,&tz); //local time
if(master_host==0 || master_timeout<=0)
{
snmp_debug(SNMPDB_SYS, "change master host to %lX",htonl(addr->remote_ip));
master_host=addr->remote_ip;
master_timeout=8;
}
if(addr->remote_ip != master_host)
{
master_timeout--;
snmp_debug(SNMPDB_SYS, "Found another master,ip=%lX.current master=%lX,timeout=%ld\n",htonl(addr->remote_ip),htonl(master_host),master_timeout);
return;
}
else
master_timeout=8;
if(abs(l_time-tv.tv_sec)<2) //compare
{
return;
}
timeSync_log("system's time's second is :%ld",tv.tv_sec);
tv.tv_sec =l_time;
settimeofday(&tv,&tz);
for(i=0;i<oidlen;i++)
{
sprintf(tmpstr,"%ld.",oid[i]);
strcat(oidstring,tmpstr);
}
timeSync_log("oid is:%s",oidstring);
timeSync_log("system's time's second is changed to %ld by %lX\n",tv.tv_sec,htonl(addr->remote_ip));
snmp_debug(SNMPDB_SYS, "system's time is changed by %lX\n",htonl(addr->remote_ip));
}
void submit_snmpcsta(BYTE *pdword,BYTE *pbyte,int len)
{
int i;
DWORD *p1,*p2;
for(i=0;i<len/4;i++)
{
p1=(DWORD *)(pdword+4*i);
p2=(DWORD *)(pbyte+4*i);
*p2=htonl(*p1);
}
}
void timeSync_log(const char *fmt,...)
{
//#if DUALSRV_DEBUG == 1
char logFile[64];
FILE *fp;
time_t l_time;
va_list ap;
char buf[8192];
struct tm *t;
char timestr[64];
l_time=time(NULL);
t=localtime(&l_time);
sprintf(logFile,"../log/SnmpTimeSync_%04d%02d%02d.log",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
fp=fopen(logFile,"a");
if(fp!=NULL)
{
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
sprintf(timestr,"%02d:%02d:%02d: ",t->tm_hour,t->tm_min,t->tm_sec);
fputs(timestr,fp);
fputs(buf,fp);
fputs("\n",fp);
fflush(fp);
va_end(ap);
fclose(fp);
}
//#endif
}

269
plat/snmp/src/snmptable.c Normal file
View File

@@ -0,0 +1,269 @@
/*
* snmp/snmptable.c
*
* Copyright (C) 2008 ADC, Inc
* Written by: Xinyu Yan <xinyu.yan@adc.com>
*
* Description: provide a mechanism to register and maintain snmp table, simplify app programing
*/
#include "./include/snmp.h"
#define MAX_SNMP_REGISTER 32 /* Maximum of modules registering snmp table */
/* It's great chance that next object and current one in same table, so cache improves searching efficiency */
static struct snmp_table_profile *cache;
static struct snmp_register *reg_cache;
static struct snmp_register snmpregister[MAX_SNMP_REGISTER];
//#define PRINT_OID
#ifdef PRINT_OID
static void printoid(u32 *oid, u8 len)
{
int i;
if(len == 0)
printf("OID length is 0!\n");
printf("%ld", oid[0]);
for(i = 1; i < len; i++)
{
printf(".%ld", oid[i]);
}
printf("\n");
}
#endif
/* To check if oid matches oid prefix */
static int oid_match_prefix(u8 prefix_len, u32 *prefix, u8 oid_len, u32 *oid)
{
if(prefix_len == 0)
return 0;
if(oid_len < prefix_len)
return 0;
if(!memcmp(prefix, oid, prefix_len*sizeof(u32)))
return 1;
else
return 0;
}
static struct snmp_register *find_snmp_register(u8 oid_len, u32 *oid)
{
int i;
struct snmp_register *reg;
if(reg_cache != NULL)
{
if(oid_match_prefix(reg_cache->prefix_len, reg_cache->prefix_oid, oid_len, oid) == 1)
return reg_cache;
}
for(i = 0; i < MAX_SNMP_REGISTER; i++)
{
reg = &snmpregister[i];
if(oid_match_prefix(reg->prefix_len, reg->prefix_oid, oid_len, oid) == 1)
{
reg_cache = reg;
return reg;
}
}
return NULL;
}
/* Check cache first, otherwise check all snmp tables by matching oid with table index */
static struct snmp_table_profile *find_snmp_table(u8 oid_len, u32 *oid)
{
int i;
struct snmp_register *reg;
struct snmp_table_profile *t;
if(cache != NULL)
{
if(oid_match_prefix(cache->index.len, cache->index.oid, oid_len, oid) == 1)
return cache;
}
if((reg = find_snmp_register(oid_len, oid)) == NULL)
return NULL;
for(i = 0; i < reg->num_of_table; i++)
{
t = &reg->table[i];
if(oid_match_prefix(t->index.len, t->index.oid, oid_len, oid) == 1)
{
cache = t;
return t;
}
}
return NULL;
}
static u32 find_next_column(u32 current_column, u32 *columns)
{
int i;
for(i = 0; i < MAX_COLUMNAR_OF_TABLE-1; i++)
{
if(current_column == columns[i])
return columns[i+1];
}
return 0;
}
static void point_to_column_and_instance(u32 **column, u32 **instance, u32 *oid, struct snmp_table_profile *t)
{
if(t->level != 0)
{
*column = oid + t->index.len + 1; /* count the entry, hence plus 1 */
}
else
{
*column = oid + t->index.len;
}
*instance = *column + 1;
}
static int table_set_response(u8 oidlen, u32 *oid, u8 *pdata, u16 datalen)
{
struct snmp_table_profile *t;
struct snmp_register *reg;
u32 *column;
u32 *instance;
if((t = find_snmp_table(oidlen, oid)) == NULL)
{
if((reg = find_snmp_register(oidlen, oid)) == NULL)
return -1;
if(reg->default_set_resp == NULL)
return -1;
else
return reg->default_set_resp(oidlen, oid, pdata, datalen);
}
point_to_column_and_instance(&column, &instance, oid, t);
if(t->set_resp == NULL)
return -1;
else
return t->set_resp(*column, instance, pdata, datalen);
}
static int table_get_response(u8 oidlen, u32 *oid, u8 *pdata, u8 *vartype)
{
struct snmp_table_profile *t;
struct snmp_register *reg;
u32 *column;
u32 *instance;
if((t = find_snmp_table(oidlen, oid)) == NULL)
{
if((reg = find_snmp_register(oidlen, oid)) == NULL)
return -1;
if(reg->default_get_resp == NULL)
return -1;
else
return reg->default_get_resp(oidlen, oid, pdata, vartype);
}
point_to_column_and_instance(&column, &instance, oid, t);
if(t->get_resp == NULL)
return -1;
else
return t->get_resp(*column, instance, pdata, vartype);
}
static int get_next_oid(u8 *oidlen, u32 *oid)
{
struct snmp_table_profile *t;
u32 *entry;
u8 entry_oid_len;
u32 *column;
u8 column_oid_len;
u32 *instance;
u8 full_oid_len;
u8 plus_one_suppress = 0;
if((t = find_snmp_table(*oidlen, oid)) == NULL)
return 0;
if(t->level == 0) /* If MIB is not table basis, do not support get next */
return 0;
entry = oid + t->index.len;
entry_oid_len = t->index.len + 1;
column = entry + 1;
column_oid_len = entry_oid_len + 1;
instance = column + 1;
full_oid_len = column_oid_len + t->level;
if(*oidlen < full_oid_len)
{
memset(oid+*oidlen, 0, (full_oid_len-*oidlen)*sizeof(u32));
if(*oidlen < entry_oid_len)
*entry = 1;
if(*oidlen < column_oid_len)
*column = t->column[0];
*oidlen = full_oid_len;
plus_one_suppress = 1;
}
for(; *column != 0; *column = find_next_column(*column, t->column))
{
if(plus_one_suppress == 0)
instance[t->level-1]++;
if(t->get_instance(instance) == 1)
return 1;
else
{
memset(instance, 0, t->level*sizeof(u32));
plus_one_suppress = 1;
}
}
return 0;
}
static int table_getnext_response(u8 prev_oidlen, u32 *prev_oid, u8 *oidlen, u32 *oid, u8 *pdata, u8 *vartype)
{
memcpy(oid, prev_oid, prev_oidlen*sizeof(u32));
*oidlen = prev_oidlen;
#ifdef PRINT_OID
printf("Prev oid: ");
printoid(oid, *oidlen);
#endif
if(get_next_oid(oidlen, oid) == 1)
{
#ifdef PRINT_OID
printf("Next oid: ");
printoid(oid, *oidlen);
#endif
return table_get_response(*oidlen, oid, pdata, vartype);
}
else
return -1;
}
int register_snmp_table(struct snmp_register *reg)
{
int i;
struct snmp_register *r;
/* TODO: check table validation */
for(i = 0; i < MAX_SNMP_REGISTER; i++)
{
r = &snmpregister[i];
if(r->prefix_len == 0)
{
memcpy(r, reg, sizeof(struct snmp_register));
inquire_getmsg(r->prefix_len, r->prefix_oid, table_get_response);
inquire_setmsg(r->prefix_len, r->prefix_oid, table_set_response);
inquire_getnextmsg(r->prefix_len, r->prefix_oid, table_getnext_response);
return 1;
}
}
return 0;
}

254
plat/snmp/src/snmptest.c Normal file
View File

@@ -0,0 +1,254 @@
#include "./include/snmp.h"
#include "./include/heartbeat.h"
static struct itimerval itimer,old_itimer;
int intval;
char strval[50]="hello!";
static void On_Timer();
static void SetTimer();
static int get_rsp(BYTE object_id_len, DWORD *object_id, BYTE *data_ptr,BYTE *vartype);
static int getnext_rsp(BYTE in_oidlen, DWORD in_oid[], BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
static int set_rsp(BYTE object_id_len, DWORD *object_id, BYTE *data_ptr,WORD len);
static void pro_rsp(snmp_pdu *pdata,snmp_addr *addr);
//static int atoi_hex(char* input);
static DWORD oid[10] ={1,3,6,1,4,1,12189,2,2,50};
//static DWORD oid[11] ={1,3,6,1,4,1,12189,2,1,50,6};
static int table_len = 9;
static int row_len = 11;
static int table_id = 50;
void showbuf(BYTE *buf,int len)
{
int i;
for(i=0;i<len;i++)
{
printf("%02X",buf[i]);
if(i>0 && i%25==0)
printf("\n");
else
printf(" ");
}
printf("\n");
}
static void showid(DWORD *buf,int len)
{
int i;
for(i=0;i<len;i++)
{
printf("%1ld.",buf[i]);
if(i>0 && i%25==0)
printf("\n");
}
printf("\n");
}
static void trap_callback(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len,snmp_addr *addr)
{
//printf("Receive trap message,oid:");
//showid(oid,oidlen);
//showbuf(pdata,len);
}
int main()
{
intval=123456;
snmp_init(4961);
heartbeat_init(0x1234);
SetTimer();
inquire_trapmsg(trap_callback);
inquire_setmsg(1,oid,set_rsp);
inquire_getmsg(1,oid,get_rsp);
inquire_rspmsg(10,oid,pro_rsp);
inquire_getnextmsg(10,oid,getnext_rsp);
//cls;
set_led(0,8);
set_alarm(0,1);
{
DWORD request_id;
snmp_pdu request_msg;
snmp_addr request_addr;
// iptrans_init();
iptrMainInit();
request_msg.pdu_type = PDU_GETBULK;
request_msg.max_repetitions = 199900;
request_msg.non_repeaters = 0;
request_msg.request_id = 10;
request_msg.var_num = 1;
sprintf(request_msg.community, "public");
memcpy(request_msg.var[0].oid, oid, 4 * 10);
request_msg.var[0].oidlen = 10;
request_addr.remote_ip =inet_addr("127.0.0.1");
request_addr.local_ip =inet_addr("172.48.234.128");
request_addr.remote_port =4961;
request_addr.local_port =4961;
request_addr.broadcast =1;
request_id = snmp_send(&request_msg, &request_addr);
printf("%ld\n",request_id);
sleep(2);
}
while(1)
{
usleep(50);
}
return 1;
}
int getnext_rsp(BYTE in_oidlen, DWORD *in_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
memcpy(oid,in_oid,in_oidlen*sizeof(DWORD));
//printf("receive command\n");
if(in_oidlen<12)
{
oid[10]=1;
oid[11]=1;
oid[12]=0;
in_oidlen=13;
}
else
{
oid[in_oidlen-1] =in_oid[in_oidlen-1]+1;
}
if(oid[in_oidlen-1]>12)
{
oid[in_oidlen-1]=0;
oid[in_oidlen-2]=in_oid[in_oidlen-2]+1;
if(oid[in_oidlen-2]>20)
return -1;
}
pdata[0] = oid[in_oidlen-1];
*vartype=2;
*oidlen = in_oidlen;
return 1;
}
void pro_rsp(snmp_pdu *pdata,snmp_addr *addr)
{
int i,getflag,value=0;
static int old_oid=0;
DWORD request_id;
snmp_pdu request_msg;
snmp_addr request_addr;
getflag = 1;
for(i=0;i<pdata->var_num;i++)
{
if(pdata->var[i].oid[table_len]!=table_id||pdata->var[i].vartype==EndOfMibView)
{
printf("\n");
getflag = 0;
break;
}
if(pdata->var[i].oid[row_len]!=old_oid)
printf("\n");
old_oid = pdata->var[i].oid[row_len];
value = decode_integer(pdata->var[i].msg,pdata->var[i].msglen);
printf("%-5d",value);
}
if(getflag)
{
request_msg.pdu_type = PDU_GETBULK;
request_msg.max_repetitions = 10;
request_msg.non_repeaters = 0;
request_msg.var_num = 1;
sprintf(request_msg.community, "public");
memcpy(request_msg.var[0].oid, pdata->var[pdata->var_num-1].oid, 4 * pdata->var[pdata->var_num-1].oidlen);
request_msg.var[0].oidlen = pdata->var[pdata->var_num-1].oidlen;
request_addr.remote_ip =addr->remote_ip;
request_addr.remote_port =addr->remote_port;
request_addr.local_port =addr->local_port;
request_id = snmp_send(&request_msg, &request_addr);
}
return;
}
void On_Timer()
{
snmp_timer();
iptrans_timer();
//heartbeat_timer();
}
void SetTimer()
{
struct sigaction act;
act.sa_handler=On_Timer;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
if(sigaction(SIGALRM,&act,NULL)<0)
{
perror("Produce Sigaction");
exit(1);
}
itimer.it_interval.tv_sec=0;
itimer.it_interval.tv_usec=1000;
itimer.it_value.tv_sec=0;
itimer.it_value.tv_usec=1000;
if (setitimer(ITIMER_REAL,&itimer,&old_itimer) !=0 )
{
printf("Setting Timer error! \n");
exit(1);
}
}
int get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
printf("I Receive get message,oid:");
showid(oid,oidlen);
/*
if(object_id[1]==2)
{
memcpy(data_ptr,(BYTE *)&intval,4);
return 4;
}
else
{
memcpy(data_ptr,(BYTE *)&strval,strlen(strval));
return strlen(strval);
}
*/
return -1;
}
int set_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len)
{
printf("I Receive set message,oid:");
showid(oid,oidlen);
showbuf(pdata,len);
/*
if(object_id[1]==2)
{
memcpy((BYTE *)&intval,data_ptr,4);
return 4;
}
else
{
memcpy((BYTE *)&strval,data_ptr,len);
return len;
}
*/
return -1;
}

239
plat/snmp/src/sysctrl.c Normal file
View File

@@ -0,0 +1,239 @@
/*****************************************************/
/*Title: sysctrl.c */
/*Descr: report operation system status */
/*Author: Liang Hanxi */
/*Create: 2002-06-24 */
/*Modify: */
/*****************************************************/
#include "./include/sysctrl.h"
#define MAX_MNT_FILESYS 255 //in CC client >16
static mnt_list MntInfo[MAX_MNT_FILESYS];
static BYTE MntNum=0;
static hb_sysinfo3 SysInfo3;
static FILE *stat_fp;
extern int errno;
static void get_mntinfo();
static BOOL get_diskinfo(long *p_total,long *p_free);
static void get_idlecpu();
static int parse_infoline(char *str,char (*row)[64],int maxrow);
void sysctrl_init()
{
struct sysinfo info;
if((stat_fp=fopen("/proc/stat","r"))==NULL)
{
printf("Fail to open /proc/stat\n");
exit(1);
}
#if 0
sysinfo(&info);
SysInfo.totalram = htonl(info.totalram/1024);
SysInfo.freeram = htonl(info.freeram/1024);
SysInfo.up_time = htonl(time(NULL));
#endif
SysInfo3.up_time = htonl(time(NULL));
get_idlecpu();
//get_mntinfo();
fclose(stat_fp);
}
void system_report()
{
static int m_state=0;
struct sysinfo info;
unsigned long totaldisk,freedisk;
switch(m_state)
{
case 50*60*10: //10min
m_state=0;
#if 0
sysinfo(&info);
SysInfo.totalram = htonl(info.totalram/1024);
SysInfo.freeram = htonl(info.freeram/1024);
SysInfo3.ram_total_used = htonl(info.totalram - info.freeram);
SysInfo3.ram_total = htonl(info.totalram);
// set_status(8,22,(BYTE *)&SysInfo.cpu_idle);
set_status(8,38,(BYTE *)&SysInfo2.start_time);
break;
#endif
default:
m_state++;
if(m_state%(50*30)==0) //30s
{
sysinfo(&info);
SysInfo3.ram_total_used = htonl((info.totalram - info.freeram)>>10);
SysInfo3.ram_total = htonl(info.totalram>>10);
SysInfo3.ram_swap_used = htonl((info.totalswap - info.freeswap)>>10);
SysInfo3.ram_swap_total = htonl(info.totalswap>>10);
SysInfo3.ram_buffers = htonl(info.bufferram>>10);
SysInfo3.ram_cached = htonl((info.freeram * info.mem_unit + info.bufferram * info.mem_unit)>>10);
get_diskinfo(&totaldisk,&freedisk);
SysInfo3.pt_count = 1;
SysInfo3.pt[0].num = 1;
SysInfo3.pt[0].pos = 0x2f;
SysInfo3.pt[0].used_space = htonl(totaldisk - freedisk);
SysInfo3.pt[0].total_space = htonl(totaldisk);
get_idlecpu();
// set_status(8,22,(BYTE *)&SysInfo.cpu_idle);
set_status(8,40,(BYTE *)&SysInfo3);
}
break;
}
}
void get_idlecpu()
{
char buf[128],row[5][64];
DWORD timeofuser,timeofsys,timeofnice,timeofidle;
static DWORD oldusertime=0,oldsystime=0,oldnicetime=0,oldidletime=0;
double idlecpu,totalcpu;
u_short idlerate;
// by simon at 20240426
/*
if(fseek(stat_fp,0,SEEK_SET)!=0)
{
snmp_debug(SNMPDB_ERR|SNMPDB_SYS, "Fail to seek to beginning of /proc/stat");
return;
}
if(fgets(buf,128,stat_fp)==NULL)
{
snmp_debug(SNMPDB_ERR|SNMPDB_SYS, "Fail to Read information from /proc/stat");
return;;
}
if(parse_infoline(buf,row,5)<5)
return;
timeofuser=strtoul(row[1],NULL,10);
timeofnice=strtoul(row[2],NULL,10);
timeofsys=strtoul(row[3],NULL,10);
timeofidle=strtoul(row[4],NULL,10);
//if((pinfo=strstr(buf,"cpu "))==NULL)
// return;
//sscanf(pinfo,"cpu %lu %lu %lu %lu",&timeofuser,&timeofnice,&timeofsys,&timeofidle);
//printf("CPU Time:%ld,%ld,%ld,%ld\n",timeofuser,timeofnice,timeofsys,timeofidle);
totalcpu=timeofuser-oldusertime+timeofnice-oldnicetime+timeofsys-oldsystime+timeofidle-oldidletime;
if(totalcpu==0)
return;
idlecpu=timeofidle-oldidletime;
idlerate=(u_short)((idlecpu/totalcpu)*1000);
//store current time
oldusertime=timeofuser;
oldnicetime=timeofnice;
oldsystime=timeofsys;
oldidletime=timeofidle;
if(idlerate<=1000)
{
SysInfo3.idle_cpu_rate = (u_short)(idlecpu * 100 / totalcpu);
}
*/
}
void get_mntinfo()
{
FILE *mntfp;
struct mntent *pmnt;
struct statfs statbuf;
mntfp = setmntent("/etc/mtab", "r");
while ((pmnt = getmntent(mntfp))!=NULL && MntNum<MAX_MNT_FILESYS)
{
if(statfs(pmnt->mnt_fsname,&statbuf)!=0)
continue;
strcpy(MntInfo[MntNum].mnt_fsname,pmnt->mnt_fsname);
strcpy(MntInfo[MntNum].mnt_dir,pmnt->mnt_dir);
MntInfo[MntNum++].limit =20*1024;
}
endmntent(mntfp);
//free(pmnt); getmntent return static pointer , not need to free
}
BOOL get_diskinfo(long *p_total,long *p_free)
{
int i;
struct statfs statbuf;
mnt_list *pmnt;
*p_total=0;
*p_free=0;
for(i=0;i<MntNum;i++)
{
pmnt=&MntInfo[i];
if(statfs(pmnt->mnt_dir,&statbuf)!=0)
continue;
pmnt->total=statbuf.f_blocks*(statbuf.f_bsize/1024);
*p_total+=pmnt->total;
pmnt->free=statbuf.f_bfree*(statbuf.f_bsize/1024);
*p_free+=pmnt->free;
if(pmnt->free<=pmnt->limit)
set_alarm(8,3);
//printf("%-15s%-15s%-10ld%-10ld\n",pmnt->mnt_fsname,pmnt->mnt_dir,pmnt->total,pmnt->free);
}
return 1;
}
int parse_infoline(char *str,char (*row)[64],int maxrow)
{
char *pstr=str;
int len,index=0,i;
BOOL black=0;
len=strlen(str);
for(i=0;i<len && index<maxrow;i++)
{
if(isspace(str[i]))
{
if(black==0)
pstr=str+i+1;
else
{
str[i]='\0';
if(strlen(pstr)>64)
pstr[63]=0;
sprintf(row[index++],"%s",pstr);
pstr=str+i+1;
}
black=0;
}
else
black=1;
}
if(black==1)
{
if(strlen(pstr)>64)
pstr[63]=0;
sprintf(row[index++],"%s",pstr);
}
return index;
}