github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/server/snmp.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package server
    17  
    18  import (
    19  	"fmt"
    20  	"net"
    21  
    22  	"github.com/cdevr/WapSNMP"
    23  	"github.com/cloudwan/gohan/schema"
    24  	"github.com/cloudwan/gohan/util"
    25  )
    26  
    27  //SNMP Process
    28  //Experimental
    29  func startSNMPProcess(server *Server) {
    30  	manager := schema.GetManager()
    31  	config := util.GetConfig()
    32  	enabled := config.GetParam("snmp", nil)
    33  	if enabled == nil {
    34  		return
    35  	}
    36  	host := config.GetString("snmp/address", "localhost:162")
    37  
    38  	path := "snmp://"
    39  	env := newEnvironment(server.db, server.keystoneIdentity)
    40  	err := env.LoadExtensionsForPath(manager.Extensions, path)
    41  	if err != nil {
    42  		log.Fatal(fmt.Sprintf("Extensions parsing error: %v", err))
    43  	}
    44  
    45  	addr, err := net.ResolveUDPAddr("udp", host)
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  
    50  	conn, err := net.ListenUDP("udp", addr)
    51  	if err != nil {
    52  		log.Fatal(err)
    53  	}
    54  
    55  	buf := make([]byte, 1024)
    56  	go func() {
    57  		defer conn.Close()
    58  		for server.running {
    59  			rlen, remote, err := conn.ReadFromUDP(buf)
    60  			if err != nil {
    61  				log.Error(fmt.Sprintf("[SNMP] failed read bytes %s", err))
    62  				return
    63  			}
    64  			decoded, err := wapsnmp.DecodeSequence(buf[:rlen])
    65  			if err != nil {
    66  				log.Error(fmt.Sprintf("[SNMP] failed decode bytes %s", err))
    67  				continue
    68  			}
    69  			infos := decoded[3].([]interface{})[4].([]interface{})[1:]
    70  			trap := map[string]string{}
    71  			for _, info := range infos {
    72  				listInfo := info.([]interface{})
    73  				oid := listInfo[1].(wapsnmp.Oid)
    74  				trap[oid.String()] = fmt.Sprintf("%v", listInfo[2])
    75  			}
    76  
    77  			context := map[string]interface{}{
    78  				"trap":   trap,
    79  				"remote": remote,
    80  			}
    81  			if err := env.HandleEvent("notification", context); err != nil {
    82  				log.Warning(fmt.Sprintf("extension error: %s", err))
    83  			}
    84  		}
    85  	}()
    86  }
    87  
    88  func stopSNMPProcess(server *Server) {
    89  
    90  }