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.

146 lines
4.1 KiB

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