当前位置: 技术问答>linux和unix
程序有点长,想请达人帮忙耐心看看......
来源: 互联网 发布时间:2016-06-08
本文导语: #include #include #include #include #include #include #include #include #include #include "test_que.h" //队列的头文件,里面定义了对队列的操作 #include #include #define num_threads 10 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int ...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "test_que.h" //队列的头文件,里面定义了对队列的操作
#include
#include
#define num_threads 10
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int numprinted = 0;
queueLK q; //定义了一个队列
void
snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len)
{
netsnmp_pdu *pdu, *response;
netsnmp_variable_list *vars;
int status;
pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, theoid, theoid_len);
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
for (vars = response->variables; vars; vars = vars->next_variable) {
numprinted++;
print_variable(vars->name, vars->name_length, vars);
}
}
if (response) {
snmp_free_pdu(response);
}
}
void
parse_oid(char objoid[])
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu,*response;
netsnmp_variable_list *vars;
oid name[MAX_OID_LEN];
size_t name_length;
oid root[MAX_OID_LEN];
size_t rootlen;
int running;
int status;
int count;
int exitval = 0;
//建立起snmp连接,一些必要的初始化措施
init_snmp("walkpro");
snmp_sess_init( &session );
session.peername = strdup("192.168.10.194");
session.version = SNMP_VERSION_2c;
session.securityName = strdup("motorola");
session.securityNameLen = strlen(session.securityName);
session.community = "public";
session.community_len = strlen(session.community);
session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
session.securityAuthProto = usmHMACMD5AuthProtocol;
session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
session.securityAuthKeyLen = USM_AUTH_KU_LEN;
//分析oid节点,将信息存储在root中
rootlen = MAX_OID_LEN;
if (snmp_parse_oid (objoid, root, &rootlen) == NULL) {
snmp_perror(objoid);
exit(1);
}
//打开之前已经初始化好的连接
SOCK_STARTUP;
ss = snmp_open(&session);
if(ss == NULL){
snmp_sess_perror("walkpro",&session);
SOCK_CLEANUP;
exit(1);
}
memmove(name,root,rootlen * sizeof(oid));
name_length = rootlen;
//建立一个get_next的pdu,封装好pdu后进行发送,并对收到的回复pdu(response)进行分析
running = 1;
while (running) {
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, name, name_length);
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
for (vars = response->variables; vars;
vars = vars->next_variable) {
if ((vars->name_length name, rootlen * sizeof(oid))
!= 0)) {
running = 0;//not part of this subtree
continue;
}
numprinted++;
print_variable(vars->name, vars->name_length, vars);
if ((vars->type != SNMP_ENDOFMIBVIEW) &&
(vars->type != SNMP_NOSUCHOBJECT) &&
(vars->type != SNMP_NOSUCHINSTANCE)) {
memmove((char *) name, (char *) vars->name,
vars->name_length * sizeof(oid));
name_length = vars->name_length;
} else
running = 0;
}
} else {
running = 0;
if (response->errstat == SNMP_ERR_NOSUCHNAME) {
printf("End of MIBn");
} else {
fprintf(stderr, "Error in packet.nReason: %sn",
snmp_errstring(response->errstat));
if (response->errindex != 0) {
fprintf(stderr, "Failed object: ");
for (count = 1, vars = response->variables;
vars && count != response->errindex;
vars = vars->next_variable, count++)
if (vars)
fprint_objid(stderr, vars->name,
vars->name_length);
fprintf(stderr, "n");
}
exitval = 2;
}
}
} else if (status == STAT_TIMEOUT) {
fprintf(stderr, "Timeout: No Response from %sn",
session.peername);
running = 0;
exitval = 1;
} else {
snmp_sess_perror("walkpro", ss);
running = 0;
exitval = 1;
}
if (response)
snmp_free_pdu(response);
}
if (numprinted == 0 && status == STAT_SUCCESS) {
snmp_get_and_print(ss, root, rootlen);
}
snmp_close(ss);
SOCK_CLEANUP;
}
void *
get_oid() //获取oid,只要队列不空,线程就从队列中取数据直到为空
{
char *ptr,*oid;
int len;
int i = 0;
pthread_mutex_lock(&mutex);
while(!emptyQueue(&q)){
ptr = outQueue(&q); //outQueue()的功能是删除一个节点,并将该节点的值返回
len = strlen(ptr);
oid = malloc(len * sizeof(char) + 1);
strncpy(oid,ptr,len);
i++;
printf("%d:oid = %sn",i,oid);
// parse_oid(oid);
}
pthread_mutex_unlock(&mutex);
free(oid);
return NULL;
}
/************************************************************************/
int
main(int argc, char* argv[])
{
int fd;
struct stat st;
char *ptr_fd;
char *ptr_oid;
char *out_ptr = NULL;
int i,j;
FILE *fp;
pthread_t *tid;
fd = open("test.txt",O_RDONLY);
fstat(fd,&st);
ptr_fd = mmap(NULL,st.st_size,PROT_READ | PROT_WRITE,MAP_PRIVATE,fd,0); //为文件建立内存映射
if(ptr_fd == MAP_FAILED){
printf("mmap failed!n");
return 0;
}
//初始化队列,并且从内存映射中,一次返回一行,建立队列
initQueue(&q);
ptr_oid = strtok_r(ptr_fd,"n",&out_ptr);
if(enQueue(&q,ptr_oid) != 0)
printf("队列初始化错误!n");
while(ptr_oid = strtok_r(NULL,"n",&out_ptr)){
if(enQueue(&q,ptr_oid) != 0)
printf("队列初始化错误!n");
}
//创建10个线程
tid = malloc(num_threads * sizeof(pthread_t));
for(i = 0; i