~ubuntu-branches/ubuntu/trusty/cpqarrayd/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
   CpqArray Deamon, a program to monitor and remotely configure a 
   SmartArray controller.
   Copyright (C) 1999-2003  Hugo Trippaers

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
   $Header: /spark/cpqarrayd/sendtrap.c,v 1.7 2007/12/03 17:06:22 spark Exp $
*/
#include "config.h"

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <fcntl.h>

#ifdef HAVE_SNMPTRAP
  #include <net-snmp/net-snmp-config.h>
  #include <net-snmp/net-snmp-includes.h>
  #undef DEMO_USE_SNMP_VERSION_3
#endif


#include "cpqarrayd.h"

int sendtrap(struct opts opts, char *community, 
	     int status, char *message)
{
  int return_stat = 0;

#ifdef HAVE_SNMPTRAP
  struct snmp_session session, *ss;
  struct snmp_pdu *pdu;    
  char statusmsg[12];
  oid enterprise[] = {1,3,6,1,4,1,300};
  oid statusoid[] = {1,3,6,1,4,1,300,1};
  oid messageoid[] = {1,3,6,1,4,1,300,2};
  int counter;
  char *messagebuf;
  in_addr_t *pdu_in_addr_t;

  
  for (counter=0; counter < opts.nr_traphosts; counter++) {
    
    snmp_sess_init( &session );
    
    session.peername = strdup(opts.traphosts[counter]);
    session.community = strdup(community);
    session.community_len = strlen(session.community);
    session.version = SNMP_VERSION_1;
    session.retries = 5; 
    session.timeout = 500;
    session.remote_port = 162;
    session.authenticator = NULL;
    
    ss = snmp_open(&session);   
    if (!ss) {
      snmp_sess_perror( "Error: sendtrap/snmp_open", &session );
      return_stat++;
    }
    
    pdu = snmp_pdu_create(SNMP_MSG_TRAP);
    pdu_in_addr_t = (struct in_addr_t *)&pdu->agent_addr;
    pdu->enterprise = malloc(sizeof(enterprise));
    memcpy (pdu->enterprise, enterprise, sizeof(enterprise));
    pdu->enterprise_length = sizeof(enterprise) / sizeof (oid);
    pdu->trap_type = 6;
    pdu->specific_type = 1; 
    pdu->time = 0;
    pdu->contextEngineID = 0x0;
    *pdu_in_addr_t = get_myaddr();

    sprintf(statusmsg, "%d", status);

    snmp_add_var (pdu, statusoid, sizeof(statusoid) / sizeof (oid), 'i', 
		  statusmsg);
    messagebuf = strdup(message);
    snmp_add_var (pdu, messageoid, sizeof(messageoid) / sizeof (oid), 's', 
		  message);
    
    if (!snmp_send(ss, pdu)) {
      snmp_sess_perror("Error: sendtrap/snmp_send", &ss );
      return_stat++;
    }
    snmp_close(ss);
  }
#endif
  return return_stat;
}