OpenWrt Forum Archive

Topic: mini_snmpd - basic init script

The content of this topic has been archived on 24 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

Configuration file ( /etc/config/mini_snmpd):

config 'mini_snmpd'
        option 'community' 'public'
        option 'location' 'spain'
        option 'contact' 'direccion@correo.es'
        option 'disks' '/tmp,/jffs'
        option 'interfaces' 'lo,br-lan,eth0.1,eth1' # Max 4 ¿?

Init script (/etc/init.d/mini_snmpd):

#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=50

mini_snmpd_config() {
        config_get community $1 community
        config_get location $1 location
        config_get contact $1 contact
        config_get disks $1 disks
        config_get interfaces $1 interfaces
}

start() {
        config_load mini_snmpd
        config_foreach mini_snmpd_config mini_snmpd

        /sbin/mini_snmpd -c $community -L $location -C $contact -d $disks -i $interfaces &
}

stop() {
        killall mini_snmpd
}

Set executable and link it:

# chmod +x /etc/init.d/mini_snmpd
# cd /etc/rc.d/
# ln -s ../init.d/mini_snmpd S50mini_snmpd

Reboot and try from remote machine (snmpwalk -c public -v 2c 192.168.1.1)

Bye.

PD: Open port 161 on firewall... only for lan.

(Last edited by opotonil on 6 Jul 2009, 20:48)

opotonil wrote:
# chmod +x /etc/init.d/mini_snmpd
# cd /etc/rc.d/
# ln -s ../init.d/mini_snmpd S50mini_snmpd

This is not correct. Instead of above use:

root@OpenWrt:~# chmod +x /etc/init.d/mini_snmpd
root@OpenWrt:~# /etc/init.d/mini_snmpd enable # Creates the symlink in /etc/rc.d/ from START=50 in the init script. Use '/etc/init.d/mini_snmpd disable' to delete the symlink.
root@OpenWrt:~# /etc/init.d/mini_snmpd start

(Last edited by Dogge on 11 Jul 2009, 11:42)

/etc/config/mini_snmpd

config 'mini_snmpd'
    option 'enabled' '1'
    option 'community' 'public'
    option 'location' 'spain'
    option 'contact' 'direccion@correo.es'
    option 'disks' '/tmp,/jffs'
    option 'interfaces' 'lo,br-lan,eth0.1,eth1' # Max 4

/etc/init.d/mini_snmpd

#!/bin/sh /etc/rc.common
# Copyright (C) 2009 OpenWrt.org

NAME=mini_snmpd
PROG=/usr/bin/$NAME
START=50

append_string() {
    local section="$1"
    local option="$2"
    local value="$3"
    local _val
    config_get _val "$section" "$option"
    [ -n "$_val" ] && append args "$3 $_val"
}

mini_snmpd_config() {
    local cfg="$1"
    args=""

    append_string "$cfg" community "-c"
    append_string "$cfg" location "-L"
    append_string "$cfg" contact "-C"
    append_string "$cfg" disks "-d"
    append_string "$cfg" interfaces "-i"
    
        config_get_bool enabled "$cfg" "enabled" '1'c
        [ "$enabled" -gt 0 ] && $PROG $args &
}

start() {
        config_load mini_snmpd
        config_foreach mini_snmpd_config mini_snmpd
}

stop() {
    killall mini_snmpd
}
root@OpenWrt:~# chmod +x /etc/init.d/mini_snmpd
root@OpenWrt:~# /etc/init.d/mini_snmpd enable # Creates the symlink in /etc/rc.d/ from START=50 in the init script. Use '/etc/init.d/mini_snmpd disable' to delete the symlink.
root@OpenWrt:~# /etc/init.d/mini_snmpd start

Maybe You can also write an LuCI application for it smile

All right, the correct is:

/etc/init.d/mini_snmpd enable

And your init script is more elegant, thanks. But the correct path to mini_snmp is "/sbin/mini_snmpd", on my case.

/etc/init.d/mini_snmpd (It is same as your script but variable names are more clear, I think)

#!/bin/sh /etc/rc.common                  
# Copyright (C) 2006 OpenWrt.org          

NAME=mini_snmpd
PROG=/sbin/$NAME
START=50        

append_param() {
        local section="$1"
        local option="$2" 
        local arg="$3"    
        local value       

        config_get value "$section" "$option"
        [ -n "$value" ] && append params "$arg $value"
}                                                     

mini_snmpd_config() {
        local cfg="$1"
        params=""

        append_param "$cfg" community "-c"
        append_param "$cfg" location "-L"
        append_param "$cfg" contact "-C"
        append_param "$cfg" disks "-d"
        append_param "$cfg" interfaces "-i"

        config_get_bool enabled "$cfg" "enabled" '1'
}

start() {
        config_load mini_snmpd
        config_foreach mini_snmpd_config mini_snmpd

        [ "$enabled" -gt 0 ] && $PROG $params &
}

stop() {
        killall mini_snmpd
}

Maybe You can also write an LuCI application for it smile

I have to look for instructions or how-to and for time... Thanks again.

(Last edited by opotonil on 11 Jul 2009, 19:41)

I've copied the configs as they appear and the $params variable seems to remain empty. I edited the /etc/init.d/mini_snmpd file as follows for transparency.

#!/bin/sh /etc/rc.common                  
# Copyright (C) 2006 OpenWrt.org          

NAME=mini_snmpd
PROG=/sbin/$NAME
START=50        

append_param() {
        local section="$1"
        local option="$2" 
        local arg="$3"    
        local value       

        config_get value "$section" "$option"
        [ -n "$value" ] && append params "$arg $value"
}                                                     

mini_snmpd_config() {
        local cfg="$1"
        params="-a "
        echo $1
        append_param "$cfg" community "-c"
        append_param "$cfg" description "-D"
        append_param "$cfg" location "-L"
        append_param "$cfg" contact "-C"
        append_param "$cfg" disks "-d"
        append_param "$cfg" interfaces "-i"
        echo test $params
        config_get_bool enabled "$cfg" "enabled" '1'
}

start() {
        config_load mini_snmpd
        config_foreach mini_snmpd_config mini_snmpd

#        [ "$enabled" -gt 0 ] && 
         echo $PROG
         echo $params
         $PROG $params &
}

stop() {
        killall mini_snmpd
}

Output from /etc/init.d/mini_snmpd start, first is "echo $PROG" second line is "echo $params":

/sbin/mini_snmpd

Here is my /etc/config/mini_snmpd

config mini_snmpd
    option enabled '1'
    option community 'blah'
    option description 'Kamikaze8.09'
    option location 'location'
    option contact 'NOC'
    option disks '/tmp,/jffs'
    option interfaces 'lo,br-lan,wl0'

(Last edited by bulldozer2003 on 15 Dec 2009, 22:31)

The discussion might have continued from here.