[packages] net-snmp: Enable UDP IPv6 transport (closes #5338)
[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_agent_add() {
13 local cfg="$1"
14
15 config_get agentaddress "$cfg" agentaddress
16 [ -n "$agentaddress" ] || return 0
17 echo "agentaddress $agentaddress" >> $RUN_C
18 }
19 snmpd_system_add() {
20 local cfg="$1"
21 config_get syslocation "$cfg" sysLocation
22 [ -n "$syslocation" ] && echo "sysLocation $syslocation" >> $RUN_C
23 config_get syscontact "$cfg" sysContact
24 [ -n "$syscontact" ] && echo "sysContact $syscontact" >> $RUN_C
25 config_get sysname "$cfg" sysName
26 [ -n "$sysname" ] && echo "sysName $sysname" >> $RUN_C
27 config_get sysservice "$cfg" sysService
28 [ -n "$sysservice" ] && echo "sysService $sysservice" >> $RUN_C
29 config_get sysdescr "$cfg" sysDescr
30 [ -n "$sysdescr" ] && echo "sysDescr $sysdescr" >> $RUN_C
31 config_get sysobjectid "$cfg" sysObjectID
32 [ -n "$sysobjectid" ] && echo "sysObjectID $sysobjectid" >> $RUN_C
33 }
34 snmpd_com2sec_add() {
35 local cfg="$1"
36 config_get secname "$cfg" secname
37 [ -n "$secname" ] || return 0
38 config_get source "$cfg" source
39 [ -n "$source" ] || return 0
40 config_get community "$cfg" community
41 [ -n "$community" ] || return 0
42 echo "com2sec $secname $source $community" >> $RUN_C
43 }
44 snmpd_group_add() {
45 local cfg="$1"
46 config_get group "$cfg" group
47 [ -n "$group" ] || return 0
48 config_get version "$cfg" version
49 [ -n "$version" ] || return 0
50 config_get secname "$cfg" secname
51 [ -n "$secname" ] || return 0
52 echo "group $group $version $secname" >> $RUN_C
53 }
54 snmpd_view_add() {
55 local cfg="$1"
56 config_get viewname "$cfg" viewname
57 [ -n "$viewname" ] || return 0
58 config_get type "$cfg" type
59 [ -n "$type" ] || return 0
60 config_get oid "$cfg" oid
61 [ -n "$oid" ] || return 0
62 # optional mask
63 config_get mask "$cfg" mask
64 echo "view $viewname $type $oid $mask" >> $RUN_C
65 }
66 snmpd_access_add() {
67 local cfg="$1"
68 config_get group "$cfg" group
69 [ -n "$group" ] || return 0
70 config_get context "$cfg" context
71 [ -n $context ] || return 0
72 [ "$context" == "none" ] && context='""'
73 config_get version "$cfg" version
74 [ -n "$version" ] || return 0
75 config_get level "$cfg" level
76 [ -n "$level" ] || return 0
77 config_get prefix "$cfg" prefix
78 [ -n "$prefix" ] || return 0
79 config_get read "$cfg" read
80 [ -n "$read" ] || return 0
81 config_get write "$cfg" write
82 [ -n "$write" ] || return 0
83 config_get notify "$cfg" notify
84 [ -n "$notify" ] || return 0
85 echo "access $group $context $version $level $prefix $read $write $notify" >> $RUN_C
86 }
87 snmpd_pass_add() {
88 local cfg="$1"
89 local pass='pass'
90
91 config_get miboid "$cfg" miboid
92 [ -n "$miboid" ] || return 0
93 config_get prog "$cfg" prog
94 [ -n "$prog" ] || return 0
95 config_get_bool persist "$cfg" persist 0
96 [ $persist -ne 0 ] && pass='pass_persist'
97 config_get priority "$cfg" priority
98 priority=${priority:+-p $priority}
99 echo "$pass $priority $miboid $prog" >> $RUN_C
100 }
101 snmpd_exec_add() {
102 local cfg="$1"
103
104 config_get execname "$cfg" execname
105 [ -n "$execname" ] || return 0
106 config_get prog "$cfg" prog
107 [ -n "$prog" ] || return 0
108 config_get args "$cfg" args
109 config_get miboid "$cfg" miboid
110 echo "exec $miboid $execname $prog $args" >> $RUN_C
111 }
112 start() {
113 [ -d $LIB_D ] || mkdir -p $LIB_D
114 [ -d $LOG_D ] || mkdir -p $LOG_D
115 [ -d $RUN_D ] || mkdir -p $RUN_D
116 [ -f $RUN_C ] && rm -f $RUN_C
117
118 config_load snmpd
119
120 config_foreach snmpd_agent_add agent
121 config_foreach snmpd_system_add system
122 config_foreach snmpd_com2sec_add com2sec
123 config_foreach snmpd_group_add group
124 config_foreach snmpd_view_add view
125 config_foreach snmpd_access_add access
126 config_foreach snmpd_pass_add pass
127 config_foreach snmpd_exec_add exec
128
129 [ -f $DEFAULT ] && . $DEFAULT
130 $DEBUG /usr/sbin/snmpd $OPTIONS
131 }
132 stop() {
133 [ -f $PID_F ] && kill $(cat $PID_F)
134 [ -f $RUN_C ] && rm -f $RUN_C
135 }