-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatrap.go
133 lines (111 loc) · 3.19 KB
/
creatrap.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"log"
"os"
"time"
g "github.com/soniah/gosnmp"
)
/*
pdu := g.SnmpPDU{
Name: "1.3.6.1.2.1.1.6",
Type: g.OctetString,
Value: "Oval Office",
}
*/
/*
device := "r-al900"
argomento := "sessioni ppp"
severity := 5
summary := "Forte abbassamento sessioni ppp"
*/
// CreaTrap invia trap snmp v1 per notificare gli eventi
func CreaTrap(
device, argomento, summary, ipdevice string, specific,
severity int) (err error) {
// Se si tratta di inviare trap per mancanza di dati
// (specific = 1 e severity > 0)
if specific == 2 && severity > 0 {
//aggiungo il device alla lista per 8 ore
nientedatippp.AddWithTTL(device, true, 8*time.Hour)
}
adesso := time.Now()
// Creo la variabile trapMancanoDatiInviata come falsa
var trapMancanoDatiInviata = false
// Verifico se il device è nella lista di quelli che non hanno dati
elements := nientedatippp.GetAll()
for el := range elements {
if el == device {
// Se è presente cambio la variabile trapMancanoDatiInviata
// su true vuol dire che è stata inviata una trap di problema
// nelle 8 ore precedenti.
trapMancanoDatiInviata = true
}
}
// Se vuoi inviare una trap per mancanza di dati
// e non sono le 10 di mattina
// oppure il problema è stato già notificato allora esce.
if specific == 2 && severity > 0 && adesso.Hour() != 10 &&
trapMancanoDatiInviata == true {
return
}
// Se invece si deve comunicare la risoluzione di un problema di tipo
// mancanza dati (specific = 1 e severity = 0)
// non importa a che ora si risolve
// e se la variabile trapMancanoDatiInviata è falsa vuol dire che
// non è stata notificata la trap del problema
if specific == 1 && severity == 0 && trapMancanoDatiInviata == false {
// quindi si esce perchè non c'è nessuna trap di risoluzione da inviare
return
}
// In tutti gli altri casi si può inviare la trap
// Default is a pointer to a GoSNMP struct that contains sensible defaults
// eg port 161, community public, etc
g.Default.Target = configuration.IPDOMSnmpReceiver
g.Default.Port = configuration.IPDOMSnmpPort
g.Default.Version = g.Version1
g.Default.Community = configuration.IPDOMSnmpCommunity
g.Default.Logger = log.New(os.Stdout, "", 0)
err = g.Default.Connect()
if err != nil {
log.Printf("Error Connect() err: %s\n", err.Error())
}
defer g.Default.Conn.Close()
pdu1 := g.SnmpPDU{
Name: "1.3.6.1.4.1.8888.200.34.34.1.1",
Type: g.OctetString,
Value: "RAMSES",
}
pdu2 := g.SnmpPDU{
Name: "1.3.6.1.4.1.8888.200.34.34.1.2",
Type: g.OctetString,
Value: device,
}
pdu3 := g.SnmpPDU{
Name: "1.3.6.1.4.1.8888.200.34.34.1.3",
Type: g.OctetString,
Value: argomento,
}
pdu4 := g.SnmpPDU{
Name: "1.3.6.1.4.1.8888.200.34.34.1.4",
Type: g.Integer,
Value: severity,
}
pdu5 := g.SnmpPDU{
Name: "1.3.6.1.4.1.8888.200.34.34.1.5",
Type: g.OctetString,
Value: summary,
}
trap := g.SnmpTrap{
Variables: []g.SnmpPDU{pdu1, pdu2, pdu3, pdu4, pdu5},
Enterprise: ".1.3.6.1.4.1.8888.200.34.34.34",
AgentAddress: ipdevice,
GenericTrap: 6,
SpecificTrap: specific,
Timestamp: 300,
}
_, err = g.Default.SendTrap(trap)
if err != nil {
log.Printf("Error SendTrap() err: %s\n", err.Error())
}
return err
}