You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
9.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2008 OpenWrt.org
  3. START=50
  4. USE_PROCD=1
  5. PROG="/usr/sbin/snmpd"
  6. CONFIGFILE="/var/run/snmpd.conf"
  7. snmpd_agent_add() {
  8. local cfg="$1"
  9. config_get agentaddress "$cfg" agentaddress
  10. [ -n "$agentaddress" ] || return 0
  11. echo "agentaddress $agentaddress" >> $CONFIGFILE
  12. }
  13. snmpd_agentx_add() {
  14. local cfg="$1"
  15. echo "master agentx" >> $CONFIGFILE
  16. config_get agentxsocket "$cfg" agentxsocket
  17. [ -n "$agentxsocket" ] && echo "agentXSocket $agentxsocket" >> $CONFIGFILE
  18. }
  19. snmpd_system_add() {
  20. local cfg="$1"
  21. config_get syslocation "$cfg" sysLocation
  22. [ -n "$syslocation" ] && echo "sysLocation $syslocation" >> $CONFIGFILE
  23. config_get syscontact "$cfg" sysContact
  24. [ -n "$syscontact" ] && echo "sysContact $syscontact" >> $CONFIGFILE
  25. config_get sysname "$cfg" sysName
  26. [ -n "$sysname" ] && echo "sysName $sysname" >> $CONFIGFILE
  27. config_get sysservice "$cfg" sysService
  28. [ -n "$sysservice" ] && echo "sysService $sysservice" >> $CONFIGFILE
  29. config_get sysdescr "$cfg" sysDescr
  30. [ -n "$sysdescr" ] && echo "sysDescr $sysdescr" >> $CONFIGFILE
  31. config_get sysobjectid "$cfg" sysObjectID
  32. [ -n "$sysobjectid" ] && echo "sysObjectID $sysobjectid" >> $CONFIGFILE
  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" >> $CONFIGFILE
  43. }
  44. snmpd_com2sec6_add() {
  45. local cfg="$1"
  46. config_get secname "$cfg" secname
  47. [ -n "$secname" ] || return 0
  48. config_get source "$cfg" source
  49. [ -n "$source" ] || return 0
  50. config_get community "$cfg" community
  51. [ -n "$community" ] || return 0
  52. echo "com2sec6 $secname $source $community" >> $CONFIGFILE
  53. }
  54. snmpd_group_add() {
  55. local cfg="$1"
  56. config_get group "$cfg" group
  57. [ -n "$group" ] || return 0
  58. config_get version "$cfg" version
  59. [ -n "$version" ] || return 0
  60. config_get secname "$cfg" secname
  61. [ -n "$secname" ] || return 0
  62. echo "group $group $version $secname" >> $CONFIGFILE
  63. }
  64. snmpd_view_add() {
  65. local cfg="$1"
  66. config_get viewname "$cfg" viewname
  67. [ -n "$viewname" ] || return 0
  68. config_get type "$cfg" type
  69. [ -n "$type" ] || return 0
  70. config_get oid "$cfg" oid
  71. [ -n "$oid" ] || return 0
  72. # optional mask
  73. config_get mask "$cfg" mask
  74. echo "view $viewname $type $oid $mask" >> $CONFIGFILE
  75. }
  76. snmpd_access_add() {
  77. local cfg="$1"
  78. config_get group "$cfg" group
  79. [ -n "$group" ] || return 0
  80. config_get context "$cfg" context
  81. [ -n $context ] || return 0
  82. [ "$context" == "none" ] && context='""'
  83. config_get version "$cfg" version
  84. [ -n "$version" ] || return 0
  85. config_get level "$cfg" level
  86. [ -n "$level" ] || return 0
  87. config_get prefix "$cfg" prefix
  88. [ -n "$prefix" ] || return 0
  89. config_get read "$cfg" read
  90. [ -n "$read" ] || return 0
  91. config_get write "$cfg" write
  92. [ -n "$write" ] || return 0
  93. config_get notify "$cfg" notify
  94. [ -n "$notify" ] || return 0
  95. echo "access $group $context $version $level $prefix $read $write $notify" >> $CONFIGFILE
  96. }
  97. snmpd_trap_hostname_add() {
  98. local cfg="$1"
  99. config_get hostname "$cfg" HostName
  100. config_get port "$cfg" Port
  101. config_get community "$cfg" Community
  102. config_get type "$cfg" Type
  103. echo "$type $hostname $community $port" >> $CONFIGFILE
  104. }
  105. snmpd_trap_ip_add() {
  106. local cfg="$1"
  107. config_get host_ip "$cfg" HostIP
  108. config_get port "$cfg" Port
  109. config_get community "$cfg" Community
  110. config_get type "$cfg" Type
  111. echo "$type $host_ip $community $port" >> $CONFIGFILE
  112. }
  113. snmpd_access_default_add() {
  114. local cfg="$1"
  115. config_get mode "$cfg" Mode
  116. config_get community "$cfg" CommunityName
  117. config_get oidrestrict "$cfg" RestrictOID
  118. config_get oid "$cfg" RestrictedOID
  119. echo -n "$mode $community default" >> $CONFIGFILE
  120. [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
  121. [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
  122. }
  123. snmpd_access_HostName_add() {
  124. local cfg="$1"
  125. config_get hostname "$cfg" HostName
  126. config_get mode "$cfg" Mode
  127. config_get community "$cfg" CommunityName
  128. config_get oidrestrict "$cfg" RestrictOID
  129. config_get oid "$cfg" RestrictedOID
  130. echo -n "$mode $community $hostname" >> $CONFIGFILE
  131. [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
  132. [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
  133. }
  134. snmpd_access_HostIP_add() {
  135. local cfg="$1"
  136. config_get host_ip "$cfg" HostIP
  137. config_get ip_mask "$cfg" IPMask
  138. config_get mode "$cfg" Mode
  139. config_get community "$cfg" CommunityName
  140. config_get oidrestrict "$cfg" RestrictOID
  141. config_get oid "$cfg" RestrictedOID
  142. echo -n "$mode $community $host_ip/$ip_mask" >> $CONFIGFILE
  143. [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
  144. [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
  145. }
  146. snmpd_pass_add() {
  147. local cfg="$1"
  148. local pass='pass'
  149. config_get miboid "$cfg" miboid
  150. [ -n "$miboid" ] || return 0
  151. config_get prog "$cfg" prog
  152. [ -n "$prog" ] || return 0
  153. config_get_bool persist "$cfg" persist 0
  154. [ $persist -ne 0 ] && pass='pass_persist'
  155. config_get priority "$cfg" priority
  156. priority=${priority:+-p $priority}
  157. echo "$pass $priority $miboid $prog" >> $CONFIGFILE
  158. }
  159. snmpd_exec_add() {
  160. local cfg="$1"
  161. config_get name "$cfg" name
  162. [ -n "$name" ] || return 0
  163. config_get prog "$cfg" prog
  164. [ -n "$prog" ] || return 0
  165. config_get args "$cfg" args
  166. config_get miboid "$cfg" miboid
  167. echo "exec $miboid $name $prog $args" >> $CONFIGFILE
  168. }
  169. snmpd_disk_add() {
  170. local cfg="$1"
  171. local disk='disk'
  172. config_get partition "$cfg" partition
  173. [ -n "$partition" ] || return 0
  174. config_get size "$cfg" size
  175. [ -n "$size" ] || return 0
  176. echo "$disk $partition $size" >> $CONFIGFILE
  177. }
  178. snmpd_engineid_add() {
  179. local cfg="$1"
  180. config_get engineid "$cfg" engineid
  181. [ -n "$engineid" ] && echo "engineID $engineid" >> $CONFIGFILE
  182. config_get engineidtype "$cfg" engineidtype
  183. [ "$engineidtype" -ge 1 -a "$engineidtype" -le 3 ] && \
  184. echo "engineIDType $engineidtype" >> $CONFIGFILE
  185. config_get engineidnic "$cfg" engineidnic
  186. [ -n "$engineidnic" ] && echo "engineIDNic $engineidnic" >> $CONFIGFILE
  187. }
  188. snmpd_sink_add() {
  189. local cfg="$1"
  190. local section="$2"
  191. local community
  192. local port
  193. local host
  194. config_get host "$cfg" host
  195. [ -n "section" -a -n "$host" ] || return 0
  196. # optional community
  197. config_get community "$cfg" community
  198. # optional port
  199. config_get port "$cfg" port
  200. port=${port:+:$port}
  201. echo "$section $host$port $community" >> $CONFIGFILE
  202. }
  203. append_parm() {
  204. local section="$1"
  205. local option="$2"
  206. local switch="$3"
  207. local _loctmp
  208. config_get _loctmp "$section" "$option"
  209. [ -z "$_loctmp" ] && return 0
  210. echo "$switch $_loctmp" >> $CONFIGFILE
  211. }
  212. append_authtrapenable() {
  213. local section="$1"
  214. local option="$2"
  215. local switch="$3"
  216. local _loctmp
  217. config_get_bool _loctmp "$section" "$option"
  218. [ -z "$_loctmp" ] && return 0
  219. [ "$_loctmp" -gt 0 ] && echo "$switch $_loctmp" >> $CONFIGFILE
  220. }
  221. snmpd_setup_fw_rules() {
  222. local net="$1"
  223. local zone
  224. zone=$(fw3 -q network "$net" 2>/dev/null)
  225. local handled_zone
  226. for handled_zone in $HANDLED_SNMP_ZONES; do
  227. [ "$handled_zone" = "$zone" ] && return
  228. done
  229. json_add_object ""
  230. json_add_string type rule
  231. json_add_string src "$zone"
  232. json_add_string proto udp
  233. json_add_string dest_port 161
  234. json_add_string target ACCEPT
  235. json_close_object
  236. HANDLED_SNMP_ZONES="$HANDLED_SNMP_ZONES $zone"
  237. }
  238. start_service() {
  239. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  240. config_load snmpd
  241. config_get_bool snmp_enabled general enabled 1
  242. [ "$snmp_enabled" -eq 0 ] && return
  243. procd_open_instance
  244. config_foreach snmpd_agent_add agent
  245. config_foreach snmpd_agentx_add agentx
  246. config_foreach snmpd_system_add system
  247. config_foreach snmpd_com2sec_add com2sec
  248. config_foreach snmpd_com2sec6_add com2sec6
  249. config_foreach snmpd_group_add group
  250. config_foreach snmpd_view_add view
  251. config_foreach snmpd_access_add access
  252. config_foreach snmpd_trap_hostname_add trap_HostName
  253. config_foreach snmpd_trap_ip_add trap_HostIP
  254. config_foreach snmpd_access_default_add access_default
  255. config_foreach snmpd_access_HostName_add access_HostName
  256. config_foreach snmpd_access_HostIP_add access_HostIP
  257. config_foreach snmpd_pass_add pass
  258. config_foreach snmpd_exec_add exec
  259. config_foreach snmpd_disk_add disk
  260. config_foreach snmpd_engineid_add engineid
  261. append_parm trapcommunity community trapcommunity
  262. config_foreach snmpd_sink_add trapsink trapsink
  263. config_foreach snmpd_sink_add trap2sink trap2sink
  264. config_foreach snmpd_sink_add informsink informsink
  265. append_authtrapenable authtrapenable enable authtrapenable
  266. append_parm v1trapaddress host v1trapaddress
  267. append_parm trapsess trapsess trapsess
  268. procd_set_param command $PROG -Lf /dev/null -f
  269. procd_set_param file $CONFIGFILE
  270. procd_set_param respawn
  271. for iface in $(ls /sys/class/net 2>/dev/null); do
  272. procd_append_param netdev "$iface"
  273. done
  274. procd_open_data
  275. json_add_array firewall
  276. config_list_foreach general network snmpd_setup_fw_rules
  277. json_close_array
  278. procd_close_data
  279. procd_close_instance
  280. }
  281. stop_service() {
  282. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  283. }
  284. service_triggers(){
  285. local script=$(readlink "$initscript")
  286. local name=$(basename ${script:-$initscript})
  287. procd_open_trigger
  288. procd_add_raw_trigger "interface.*" 2000 /etc/init.d/$name reload
  289. procd_close_trigger
  290. procd_add_reload_trigger 'snmpd'
  291. }