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.

249 lines
6.9 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. start_service() {
  179. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  180. procd_open_instance
  181. config_load snmpd
  182. config_foreach snmpd_agent_add agent
  183. config_foreach snmpd_agentx_add agentx
  184. config_foreach snmpd_system_add system
  185. config_foreach snmpd_com2sec_add com2sec
  186. config_foreach snmpd_com2sec6_add com2sec6
  187. config_foreach snmpd_group_add group
  188. config_foreach snmpd_view_add view
  189. config_foreach snmpd_access_add access
  190. config_foreach snmpd_trap_hostname_add trap_HostName
  191. config_foreach snmpd_trap_ip_add trap_HostIP
  192. config_foreach snmpd_access_default_add access_default
  193. config_foreach snmpd_access_HostName_add access_HostName
  194. config_foreach snmpd_access_HostIP_add access_HostIP
  195. config_foreach snmpd_pass_add pass
  196. config_foreach snmpd_exec_add exec
  197. config_foreach snmpd_disk_add disk
  198. procd_set_param command $PROG -Lf /dev/null -f
  199. procd_set_param file $CONFIGFILE
  200. procd_set_param respawn
  201. for iface in $(ls /sys/class/net 2>/dev/null); do
  202. procd_append_param netdev "$iface"
  203. done
  204. procd_close_instance
  205. }
  206. stop_service() {
  207. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  208. }
  209. service_triggers(){
  210. local script=$(readlink "$initscript")
  211. local name=$(basename ${script:-$initscript})
  212. procd_open_trigger
  213. procd_add_raw_trigger "interface.*" 2000 /etc/init.d/$name reload
  214. procd_close_trigger
  215. procd_add_reload_trigger 'snmpd'
  216. }