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.

171 lines
4.5 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_pass_add() {
  92. local cfg="$1"
  93. local pass='pass'
  94. config_get miboid "$cfg" miboid
  95. [ -n "$miboid" ] || return 0
  96. config_get prog "$cfg" prog
  97. [ -n "$prog" ] || return 0
  98. config_get_bool persist "$cfg" persist 0
  99. [ $persist -ne 0 ] && pass='pass_persist'
  100. config_get priority "$cfg" priority
  101. priority=${priority:+-p $priority}
  102. echo "$pass $priority $miboid $prog" >> $CONFIGFILE
  103. }
  104. snmpd_exec_add() {
  105. local cfg="$1"
  106. config_get name "$cfg" name
  107. [ -n "$name" ] || return 0
  108. config_get prog "$cfg" prog
  109. [ -n "$prog" ] || return 0
  110. config_get args "$cfg" args
  111. config_get miboid "$cfg" miboid
  112. echo "exec $miboid $name $prog $args" >> $CONFIGFILE
  113. }
  114. snmpd_disk_add() {
  115. local cfg="$1"
  116. local disk='disk'
  117. config_get partition "$cfg" partition
  118. [ -n "$partition" ] || return 0
  119. config_get size "$cfg" size
  120. [ -n "$size" ] || return 0
  121. echo "$disk $partition $size" >> $CONFIGFILE
  122. }
  123. start_service() {
  124. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  125. procd_open_instance
  126. config_load snmpd
  127. config_foreach snmpd_agent_add agent
  128. config_foreach snmpd_system_add system
  129. config_foreach snmpd_com2sec_add com2sec
  130. config_foreach snmpd_com2sec6_add com2sec6
  131. config_foreach snmpd_group_add group
  132. config_foreach snmpd_view_add view
  133. config_foreach snmpd_access_add access
  134. config_foreach snmpd_pass_add pass
  135. config_foreach snmpd_exec_add exec
  136. config_foreach snmpd_disk_add disk
  137. procd_set_param command $PROG -Lf /dev/null -f
  138. procd_set_param file $CONFIGFILE
  139. procd_set_param respawn
  140. for iface in $(ls /sys/class/net 2>/dev/null); do
  141. procd_append_param netdev "$iface"
  142. done
  143. procd_close_instance
  144. }
  145. stop_service() {
  146. [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
  147. }