[packages] net-snmp: Add option 'execname' to allow the usage of same exec names
[openwrt/svn-archive/archive.git] / libs / net-snmp / files / snmpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 OpenWrt.org
3 START=50
4
5 DEFAULT=/etc/default/snmpd
6 LIB_D=/var/lib/snmp
7 LOG_D=/var/log
8 RUN_D=/var/run
9 PID_F=$RUN_D/snmpd.pid
10 RUN_C=$RUN_D/snmpd.conf
11
12 snmpd_system_add() {
13 local cfg="$1"
14 config_get syslocation "$cfg" sysLocation
15 [ -n "$syslocation" ] && echo "sysLocation $syslocation" >> $RUN_C
16 config_get syscontact "$cfg" sysContact
17 [ -n "$syscontact" ] && echo "sysContact $syscontact" >> $RUN_C
18 config_get sysname "$cfg" sysName
19 [ -n "$sysname" ] && echo "sysName $sysname" >> $RUN_C
20 config_get sysservice "$cfg" sysService
21 [ -n "$sysservice" ] && echo "sysService $sysservice" >> $RUN_C
22 config_get sysdescr "$cfg" sysDescr
23 [ -n "$sysdescr" ] && echo "sysDescr $sysdescr" >> $RUN_C
24 config_get sysobjectid "$cfg" sysObjectID
25 [ -n "$sysobjectid" ] && echo "sysObjectID $sysobjectid" >> $RUN_C
26 }
27 snmpd_com2sec_add() {
28 local cfg="$1"
29 config_get secname "$cfg" secname
30 [ -n "$secname" ] || return 0
31 config_get source "$cfg" source
32 [ -n "$source" ] || return 0
33 config_get community "$cfg" community
34 [ -n "$community" ] || return 0
35 echo "com2sec $secname $source $community" >> $RUN_C
36 }
37 snmpd_group_add() {
38 local cfg="$1"
39 config_get group "$cfg" group
40 [ -n "$group" ] || return 0
41 config_get version "$cfg" version
42 [ -n "$version" ] || return 0
43 config_get secname "$cfg" secname
44 [ -n "$secname" ] || return 0
45 echo "group $group $version $secname" >> $RUN_C
46 }
47 snmpd_view_add() {
48 local cfg="$1"
49 config_get viewname "$cfg" viewname
50 [ -n "$viewname" ] || return 0
51 config_get type "$cfg" type
52 [ -n "$type" ] || return 0
53 config_get oid "$cfg" oid
54 [ -n "$oid" ] || return 0
55 # optional mask
56 config_get mask "$cfg" mask
57 echo "view $viewname $type $oid $mask" >> $RUN_C
58 }
59 snmpd_access_add() {
60 local cfg="$1"
61 config_get group "$cfg" group
62 [ -n "$group" ] || return 0
63 config_get context "$cfg" context
64 [ -n $context ] || return 0
65 [ "$context" == "none" ] && context='""'
66 config_get version "$cfg" version
67 [ -n "$version" ] || return 0
68 config_get level "$cfg" level
69 [ -n "$level" ] || return 0
70 config_get prefix "$cfg" prefix
71 [ -n "$prefix" ] || return 0
72 config_get read "$cfg" read
73 [ -n "$read" ] || return 0
74 config_get write "$cfg" write
75 [ -n "$write" ] || return 0
76 config_get notify "$cfg" notify
77 [ -n "$notify" ] || return 0
78 echo "access $group $context $version $level $prefix $read $write $notify" >> $RUN_C
79 }
80 snmpd_pass_add() {
81 local cfg="$1"
82 local pass='pass'
83
84 config_get miboid "$cfg" miboid
85 [ -n "$miboid" ] || return 0
86 config_get prog "$cfg" prog
87 [ -n "$prog" ] || return 0
88 config_get_bool persist "$cfg" persist 0
89 [ $persist -ne 0 ] && pass='pass_persist'
90 config_get priority "$cfg" priority
91 priority=${priority:+-p $priority}
92 echo "$pass $priority $miboid $prog" >> $RUN_C
93 }
94 snmpd_exec_add() {
95 local cfg="$1"
96
97 config_get execname "$cfg" execname
98 [ -n "$execname" ] || return 0
99 config_get prog "$cfg" prog
100 [ -n "$prog" ] || return 0
101 config_get args "$cfg" args
102 config_get miboid "$cfg" miboid
103 echo "exec $miboid $execname $prog $args" >> $RUN_C
104 }
105 start() {
106 [ -d $LIB_D ] || mkdir -p $LIB_D
107 [ -d $LOG_D ] || mkdir -p $LOG_D
108 [ -d $RUN_D ] || mkdir -p $RUN_D
109 [ -f $RUN_C ] && rm -f $RUN_C
110
111 config_load snmpd
112
113 config_foreach snmpd_system_add system
114 config_foreach snmpd_com2sec_add com2sec
115 config_foreach snmpd_group_add group
116 config_foreach snmpd_view_add view
117 config_foreach snmpd_access_add access
118 config_foreach snmpd_pass_add pass
119 config_foreach snmpd_exec_add exec
120
121 [ -f $DEFAULT ] && . $DEFAULT
122 $DEBUG /usr/sbin/snmpd $OPTIONS
123 }
124 stop() {
125 [ -f $PID_F ] && kill $(cat $PID_F)
126 [ -f $RUN_C ] && rm -f $RUN_C
127 }