mini_snmpd: import 1.2b from oldpackages for reference http://git.openwrt.org/?p...
[feed/packages.git] / net / mini_snmpd / files / mini_snmpd.init-v2
diff --git a/net/mini_snmpd/files/mini_snmpd.init-v2 b/net/mini_snmpd/files/mini_snmpd.init-v2
new file mode 100644 (file)
index 0000000..9b81407
--- /dev/null
@@ -0,0 +1,146 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2009-2016 OpenWrt.org
+
+START=95
+USE_PROCD=1
+PROCD_DEBUG=1
+PROG=/usr/bin/mini_snmpd
+NAME=mini_snmpd
+
+# mini_snmpd now starts later in the game. Expects filesystems monitored to be already mounted, 
+# and at least configuration entry for network physical interface defined in /etc/config/network
+# It handles network interfaces not yet present (e.g. ppp) but will statfs() the root/wrong filesystem if device not mounted
+
+append_disk() {
+       local disk="$1"
+       [ -z $disk_count ] && disk_count=1
+       if grep -qF "$disk" /proc/mounts ; then
+               if [ $disk_count -le 4 ] ;  then  
+                       append disks "$disk" ','
+                       disk_count=$((disk_count++))
+               else
+                       logger -s -t mini_snmpd -p daemon.error "more than 4 mountpoints defined in uci. Disc $disk ignored."
+               fi
+       else
+               logger -s -t mini_snmpd -p daemon.error "mountpoint $disk for snmp monitoring not mounted, ignoring."
+       fi
+}
+
+append_interface() {
+       local name="$1"
+       local device
+       [ $(ubus -S call network.interface dump | jsonfilter -e "@.interface[@.interface=\"$name\"].device") ] && { 
+               append interfaces "${device:-$name}" ','
+       }
+}
+
+append_arg() {
+        local cfg="$1"
+        local var="$2"
+        local opt="$3"
+        local def="$4"
+        local val
+        config_get val "$cfg" "$var"
+        [ -n "$val" -o -n "$def" ] && procd_append_param command $opt "${val:-$def}"
+}
+
+listen_interface_status() {
+       local cfg="$1"
+}
+
+watch_interfaces() {
+       local cfg="$1"
+       local enabled listen_interface
+       config_get_bool enabled "$cfg" "enabled" '1'
+       [ "$enabled" -gt 0 ] || return 0
+       config_get listen_interface "$cfg" listen_interface
+       [ "$listen_interface" = "all" ] && return 0
+       local listen_interface_up=$(ubus -S call network.interface dump | jsonfilter -e "@.interface[@.interface=\"$listen_interface\"].up")
+       # if ubus knows about it regardless if it's status we'll watch for changes.
+       [ -n "$listen_interface" -a -n "$listen_interface_up" ] && trigger_interfaces=" ${listen_interface} ${trigger_interfaces}"
+}
+
+service_triggers() {
+        #procd_add_reload_trigger mini_snmpd
+        procd_open_trigger
+        procd_add_config_trigger "config.change" "mini_snmpd" /etc/init.d/mini_snmpd reload
+        config_load 'mini_snmpd'
+        config_foreach watch_interfaces 'mini_snmpd' 
+        [ -n "${trigger_interfaces}" ] & {
+                for n in $trigger_interfaces ; do
+                       procd_add_interface_trigger "interface.*" $n /etc/init.d/mini_snmpd restart
+                done
+        }
+        procd_close_trigger
+        #procd_add_validation validate_section_mini_snmpd
+}
+
+
+start_instance() {
+       local cfg="$1" disks="" interfaces=""
+       local ipv6 debug auth
+
+       config_get_bool enabled "$cfg" "enabled" '1'
+       [ "$enabled" -gt 0 ] || return 1 
+
+       local listen_interface listen_interface_json listen_interface_ip listen_interface_device listen_interface_up
+       config_get_bool ipv6 "$cfg" "ipv6" '0'
+       config_get listen_interface "$cfg" listen_interface
+       [ -n "$listen_interface" -a "$listen_interface" != "all" ] && {
+               listen_interface_json=$(ubus -S call network.interface.$listen_interface status)
+               [ -z "$listen_interface_json" ] && { 
+                       logger -s -t mini_snmpd -p daemon.error "interface configured to bind to is not configured in /etc/config/network"
+                       return 1
+               }
+               listen_interface_up=$(jsonfilter -s "$listen_interface_json" -e '@.up')
+               if [ "$ipv6" -gt 0 ]; then
+                       listen_interface_ip=$(jsonfilter -s "$listen_interface_json" -e "@['ipv6-address'][0].address")
+               else
+                       listen_interface_ip=$(jsonfilter -s "$listen_interface_json" -e "@['ipv4-address'][0].address")
+               fi
+               [ -n "$listen_interface_ip" -a "$listen_interface_up" = 'true' ] || {
+                       logger -s -t mini_snmpd -p daemon.debug "interface configured to bind to is not up yet, procd will try again later"
+                       return 0
+               }
+               listen_interface_device=$(jsonfilter -s "$listen_interface_json" -e '@.l3_device')
+       }
+
+       procd_open_instance
+
+       procd_set_param command "$PROG" -n
+       procd_set_param stdout "1"
+       procd_set_param stderr "1"
+       procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
+
+       append_arg "$cfg" community "-c"
+       append_arg "$cfg" location "-L"
+       append_arg "$cfg" contact "-C"
+       append_arg "$cfg" udp_port "-p"
+       append_arg "$cfg" tcp_port "-P"
+       append_arg "$cfg" vendor_oid "-V"
+       append_arg "$cfg" mib_timeout "-t"
+
+       [ "$ipv6" -gt 0 ] && procd_append_param command "-6"
+       config_get_bool debug "$cfg" "debug" '0'
+       [ "$debug" -gt 0 ] && procd_append_param command "-v"
+       config_get_bool auth "$cfg" "auth" '0'
+       [ "$auth" -gt 0 ] && procd_append_param command "-a"
+       config_list_foreach "$cfg" 'disks' append_disk
+       [ -n "$disks" ] && procd_append_param command "-d $disks" 
+       config_list_foreach "$cfg" 'interfaces' append_interface
+       [ -n "$interfaces" ] && procd_append_param command "-i $interfaces"
+       # https://github.com/troglobit/mini-snmpd/issues/4 - yes I know there is no space after the -I
+       [ -n "$listen_interface_device" ] && procd_append_param command "-I$listen_interface_device"
+
+       procd_close_instance
+}
+
+start_service() {
+       config_load 'mini_snmpd'
+       config_foreach start_instance 'mini_snmpd'
+}
+
+reload_service() {
+       stop
+        start
+}