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.

241 lines
6.6 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_system_add() {
  14. local cfg="$1"
  15. config_get syslocation "$cfg" sysLocation
  16. [ -n "$syslocation" ] && echo "sysLocation $syslocation" >> $CONFIGFILE
  17. config_get syscontact "$cfg" sysContact
  18. [ -n "$syscontact" ] && echo "sysContact $syscontact" >> $CONFIGFILE
  19. config_get sysname "$cfg" sysName
  20. [ -n "$sysname" ] && echo "sysName $sysname" >> $CONFIGFILE
  21. config_get sysservice "$cfg" sysService
  22. [ -n "$sysservice" ] && echo "sysService $sysservice" >> $CONFIGFILE
  23. config_get sysdescr "$cfg" sysDescr
  24. [ -n "$sysdescr" ] && echo "sysDescr $sysdescr" >> $CONFIGFILE
  25. config_get sysobjectid "$cfg" sysObjectID
  26. [ -n "$sysobjectid" ] && echo "sysObjectID $sysobjectid" >> $CONFIGFILE
  27. }
  28. snmpd_com2sec_add() {
  29. local cfg="$1"
  30. config_get secname "$cfg" secname
  31. [ -n "$secname" ] || return 0
  32. config_get source "$cfg" source
  33. [ -n "$source" ] || return 0
  34. config_get community "$cfg" community
  35. [ -n "$community" ] || return 0
  36. echo "com2sec $secname $source $community" >> $CONFIGFILE
  37. }
  38. snmpd_com2sec6_add() {
  39. local cfg="$1"
  40. config_get secname "$cfg" secname
  41. [ -n "$secname" ] || return 0
  42. config_get source "$cfg" source
  43. [ -n "$source" ] || return 0
  44. config_get community "$cfg" community
  45. [ -n "$community" ] || return 0
  46. echo "com2sec6 $secname $source $community" >> $CONFIGFILE
  47. }
  48. snmpd_group_add() {
  49. local cfg="$1"
  50. config_get group "$cfg" group
  51. [ -n "$group" ] || return 0
  52. config_get version "$cfg" version
  53. [ -n "$version" ] || return 0
  54. config_get secname "$cfg" secname
  55. [ -n "$secname" ] || return 0
  56. echo "group $group $version $secname" >> $CONFIGFILE
  57. }
  58. snmpd_view_add() {
  59. local cfg="$1"
  60. config_get viewname "$cfg" viewname
  61. [ -n "$viewname" ] || return 0
  62. config_get type "$cfg" type
  63. [ -n "$type" ] || return 0
  64. config_get oid "$cfg" oid
  65. [ -n "$oid" ] || return 0
  66. # optional mask
  67. config_get mask "$cfg" mask
  68. echo "view $viewname $type $oid $mask" >> $CONFIGFILE
  69. }
  70. snmpd_access_add() {
  71. local cfg="$1"
  72. config_get group "$cfg" group
  73. [ -n "$group" ] || return 0
  74. config_get context "$cfg" context
  75. [ -n $context ] || return 0
  76. [ "$context" == "none" ] && context='""'
  77. config_get version "$cfg" version
  78. [ -n "$version" ] || return 0
  79. config_get level "$cfg" level
  80. [ -n "$level" ] || return 0
  81. config_get prefix "$cfg" prefix
  82. [ -n "$prefix" ] || return 0
  83. config_get read "$cfg" read
  84. [ -n "$read" ] || return 0
  85. config_get write "$cfg" write
  86. [ -n "$write" ] || return 0
  87. config_get notify "$cfg" notify
  88. [ -n "$notify" ] || return 0
  89. echo "access $group $context $version $level $prefix $read $write $notify" >> $CONFIGFILE
  90. }
  91. snmpd_trap_hostname_add() {
  92. local cfg="$1"
  93. config_get hostname "$cfg" HostName
  94. config_get port "$cfg" Port
  95. config_get community "$cfg" Community
  96. config_get type "$cfg" Type
  97. echo "$type $hostname $community $port" >> $CONFIGFILE
  98. }
  99. snmpd_trap_ip_add() {
  100. local cfg="$1"
  101. config_get host_ip "$cfg" HostIP
  102. config_get port "$cfg" Port
  103. config_get community "$cfg" Community
  104. config_get type "$cfg" Type
  105. echo "$type $host_ip $community $port" >> $CONFIGFILE
  106. }
  107. snmpd_access_default_add() {
  108. local cfg="$1"
  109. config_get mode "$cfg" Mode
  110. config_get community "$cfg" CommunityName
  111. config_get oidrestrict "$cfg" RestrictOID
  112. config_get oid "$cfg" RestrictedOID
  113. echo -n "$mode $community default" >> $CONFIGFILE
  114. [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
  115. [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
  116. }
  117. snmpd_access_HostName_add() {
  118. local cfg="$1"
  119. config_get hostname "$cfg" HostName
  120. config_get mode "$cfg" Mode
  121. config_get community "$cfg" CommunityName
  122. config_get oidrestrict "$cfg" RestrictOID
  123. config_get oid "$cfg" RestrictedOID
  124. echo -n "$mode $community $hostname" >> $CONFIGFILE
  125. [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
  126. [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
  127. }
  128. snmpd_access_HostIP_add() {
  129. local cfg="$1"
  130. config_get host_ip "$cfg" HostIP
  131. config_get ip_mask "$cfg" IPMask
  132. config_get mode "$cfg" Mode
  133. config_get community "$cfg" CommunityName
  134. config_get oidrestrict "$cfg" RestrictOID
  135. config_get oid "$cfg" RestrictedOID
  136. echo -n "$mode $community $host_ip/$ip_mask" >> $CONFIGFILE
  137. [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
  138. [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
  139. }
  140. snmpd_pass_add() {
  141. local cfg="$1"
  142. local pass='pass'
  143. config_get miboid "$cfg" miboid
  144. [ -n "$miboid" ] || return 0
  145. config_get prog "$cfg" prog
  146. [ -n "$prog" ] || return 0
  147. config_get_bool persist "$cfg" persist 0
  148. [ $persist -ne 0 ] && pass='pass_persist'
  149. config_get priority "$cfg" priority
  150. priority=${priority:+-p $priority}
  151. echo "$pass $priority $miboid $prog" >> $CONFIGFILE
  152. }
  153. snmpd_exec_add() {
  154. local cfg="$1"
  155. config_get name "$cfg" name
  156. [ -n "$name" ] || return 0
  157. config_get prog "$cfg" prog
  158. [ -n "$prog" ] || return 0
  159. config_get args "$cfg" args
  160. config_get miboid "$cfg" miboid
  161. echo "exec $miboid $name $prog $args" >> $CONFIGFILE
  162. }
  163. snmpd_disk_add() {
  164. local cfg="$1"
  165. local disk='disk'
  166. config_get partition "$cfg" partition
  167. [ -n "$partition" ] || return 0
  168. config_get size "$cfg" size
  169. [ -n "$size" ] || return 0
  170. echo "$disk $partition $size" >> $CONFIGFILE
  171. }
  172. start_service() {
  173. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  174. procd_open_instance
  175. config_load snmpd
  176. config_foreach snmpd_agent_add agent
  177. config_foreach snmpd_system_add system
  178. config_foreach snmpd_com2sec_add com2sec
  179. config_foreach snmpd_com2sec6_add com2sec6
  180. config_foreach snmpd_group_add group
  181. config_foreach snmpd_view_add view
  182. config_foreach snmpd_access_add access
  183. config_foreach snmpd_trap_hostname_add trap_HostName
  184. config_foreach snmpd_trap_ip_add trap_HostIP
  185. config_foreach snmpd_access_default_add access_default
  186. config_foreach snmpd_access_HostName_add access_HostName
  187. config_foreach snmpd_access_HostIP_add access_HostIP
  188. config_foreach snmpd_pass_add pass
  189. config_foreach snmpd_exec_add exec
  190. config_foreach snmpd_disk_add disk
  191. procd_set_param command $PROG -Lf /dev/null -f
  192. procd_set_param file $CONFIGFILE
  193. procd_set_param respawn
  194. for iface in $(ls /sys/class/net 2>/dev/null); do
  195. procd_append_param netdev "$iface"
  196. done
  197. procd_close_instance
  198. }
  199. stop_service() {
  200. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  201. }
  202. service_triggers(){
  203. local script=$(readlink "$initscript")
  204. local name=$(basename ${script:-$initscript})
  205. procd_open_trigger
  206. procd_add_raw_trigger "interface.*" 2000 /etc/init.d/$name reload
  207. procd_close_trigger
  208. procd_add_reload_trigger 'snmpd'
  209. }