[INFO] SNMP for luci-statistics

For those who might want to add SNMP to luci statistics
nano /usr/bin/stat-genconfig


function config_snmp( c )
 local str = ""


 for s in pairs(sections) do



     for key, type in pairs({ Sdata="collectd_snmp_data", Shost="collectd_snmp_host" }) do
             if sections[s][".type"] == type then

                     dname = sections[s].dname
                     Table = sections[s].Table
                     Type = sections[s].Type
                     Instance = sections[s].Instance
                     Values  = sections[s].Values
                     Values2 = sections[s].Values2
                     Values3 = sections[s].Values3
                     Values4 = sections[s].Values4
                     
                     hname = sections[s].hname
                     Address = sections[s].Address
                     Version = sections[s].Version
                     Interval = sections[s].Interval
                     Community = sections[s].Community
                     Timeout = sections[s].Timeout
                     Retries = sections[s].Retries
                     Collect = sections[s].Collect
                     Collect2 = sections[s].Collect2
                     Collect3 = sections[s].Collect3
                     Collect4 = sections[s].Collect4
                     
       

                     if dname then
                             
                                     str = str .. "\t" .. "<Data " .."\"".. dname .."\"".." >" .. " \n"
                                     str = str .. "\t" .. "Type" .. "  " .. Type .. "\n"
                                     str = str .. "\t" .. "Table" .. " " .. Table .. "\n"
                                     str = str .. "\t" .. "Instance" .. " \"" .. Instance .. "\"\n"
                                    
                                     if Vaules4 then
                                                    str = str .. "\t" .. "Values" .. " \"" .. Values .. "\" \"" .. Values2 .. "\" \"" .. Values3 .. "\" \"" .. Values4 .. "\"\n"
                                          else
                                              if Values3 then
                                                            str = str .. "\t" .. "Values" .. " \"" .. Values .. "\" \"" .. Values2 .. "\" \"" .. Values3 .. "\"\n"         
                                               else
                                                   if Values2 then
                                                            str = str .. "\t" .. "Values" .. " \"" .. Values .. "\" \"" .. Values2 .. "\"\n"
                                                     else
                                                          str = str .. "\t" .. "Values" .. " \"" .. Values .. "\"\n"
                                                 
                                              end                                        
                                          end         
                                       end
                                      str = str .. "\t" .. "</Data>" .. " \n"
                      
                                  end                  
                                                    
                                
                                    
                            

                   if hname then            
                                    str = str .. "\t" .. "<Host " .."\"".. hname .."\"".." >" .. " \n"
                                    str = str .. "\t" .. "Address" .. " \"" .. Address .. "\"\n"
                                    str = str .. "\t" .. "Version" .. " " .. Version .. "\n"
                                     str = str .. "\t" .. "Community" .. " \"" .. Community .. "\"\n"
                                    str = str .. "\t" .. "Interval" .. " " .. Interval .. "\n"
                      
                                     if Collect4 then
                                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\" \"" .. Collect2 .. "\" \"" .. Collect3 .. "\" \"" .. Collect4 .. "\"\n"
                                          else
                                              if Collect3 then
                                                            str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\" \"" .. Collect2 .. "\" \"" .. Collect3 .. "\"\n"
                                               else
                                                   if Collect2 then
                                                            str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\"\"" .. Collect2 .. "\"\n"
                                                     else
                                                          str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\"\n"
                                                  
                                                   end
                                              end
                                       end
                           
                                    str = str .. "\t" .. "Timeout" .. " " .. Timeout .. "\n"
                                    str = str .. "\t" .. "Retries" .. " " .. Retries .. "\n"
                                    
                                    str = str .. "\t" .. "</Host>" .. " \n"
                             end
                             end
                  

                     end
     end
 return str

end

and add this to plugin section

  snmp = config_snmp,

then -- nano /usr/lib/lua/luci/model/cbi/luci_statistics/snmp.lua

-- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
-- Licensed to the shostlic under the Apache License 2.0.

m = Map("luci_statistics",
        translate("SNMP Data"),
        translate(
                "SNMP DATA" 
                
        ))



-- collectd_snmp config section
s = m:section( NamedSection, "collectd_snmp", "luci_statistics" )

-- collectd_snmp_data.enable
enable = s:option( Flag, "enable", translate("Enable this plugin") )
enable.default = 0


-- collectd_snmp_data config section (Sdata)
sdata = m:section( TypedSection, "collectd_snmp_data",
        translate("SNMP Data"),
        translate(
                "This section defines the SNMP Data " 
               
        ))
sdata.addremove = true
sdata.anonymous = true


-- collectd_snmp_data.dname
sdata_name = sdata:option( Value, "dname", translate("SNMP Data Name"))
sdata_name.default = "Data1"


-- collectd_snmp_data.Type
sdata_Type = sdata:option( Value, "Type", translate("Type"))
sdata_Type.default = "if_octets"


-- collectd_snmp_data.Table
sdata_Table = sdata:option( Value, "Table", translate("Table True/False") )
---sdata_port.isinteger = true
sdata_Table.default   = "true"


-- collectd_snmp_data.Values
sdata_Values = sdata:option( Value, "Values", translate("MIBs/OIDs-1") )
---sdata_user.isinteger = true
sdata_Values.default   = "IF-MIB::ifInOctets"

-- collectd_snmp_data.Values2
sdata_Values2 = sdata:option( Value, "Values2", translate("MIBs/OIDs-2") )
sdata_Values2.default = "IF-MIB::ifOutOctets"
sdata_Values2.optional  = true

-- collectd_snmp_data.Values3
sdata_Values3 = sdata:option( Value, "Values3", translate("MIBs/OIDs-3") )
sdata_Values3.default = ""
sdata_Values3.optional  = true

-- collectd_snmp_data.Values4
sdata_Values4 = sdata:option( Value, "Values4", translate("MIBs/OIDs-4") )
sdata_Values4.default = ""
sdata_Values4.optional  = true

-- collectd_snmp_data.Instance
sdata_Instance = sdata:option( Value, "Instance", translate("Instance") )
sdata_Instance.isinteger = true
sdata_Instance.default   = "IF-MIB::ifDescr"


-- collectd_snmp_host config section (Shost)
shost = m:section( TypedSection, "collectd_snmp_host",
        translate("SNMP Hosts"),
        translate(
                "This section defines to SNMP Hosts " 
                
        ))
shost.addremove = true
shost.anonymous = true

-- collectd_snmp_host.hname
shost_name = shost:option( Value, "hname", translate("SNMP Host Name"))
shost_name.default = "DATA1"


-- collectd_snmp_host.Address
shost_Address = shost:option( Value, "Address", translate("Host Address") )
shost_Address.default = "ADDRESS"

-- collectd_snmp_host.Version
shost_Version = shost:option( Value, "Version", translate("Version 1,2 or 3 ") )
shost_Version.default   = "1"
shost_Version.isinteger = true
--shost_Version.optional  = true

-- collectd_snmp_host.Interval
shost_Interval = shost:option( Value, "Interval", translate("Interval") )
shost_Interval.default = "60"

-- collectd_snmp_host.Collect
shost_Collect = shost:option( Value, "Collect", translate("Data Name") )
shost_Collect.default = "Data1"

-- collectd_snmp_host.Community
shost_Community = shost:option( Value, "Community", translate("Community") )
shost_Community.default = "Public"
shost_Community.optional  = true

-- collectd_snmp_host.Timeout
shost_Timeout = shost:option( Value, "Timeout", translate("Timeout") )
shost_Timeout.default = "10"
shost_Timeout.optional  = true

-- collectd_snmp_host.Retries
shost_Retries = shost:option( Value, "Retries", translate("Retries") )
shost_Retries.default = "1"
shost_Retries.optional  = true

-- collectd_snmp_host.Collect2
shost_Collect2 = shost:option( Value, "Collect2", translate("Data Name2") )
shost_Collect2.default = ""
shost_Collect2.optional  = true

-- collectd_snmp_host.Collect3
shost_Collect3 = shost:option( Value, "Collect3", translate("Data Name3") )
shost_Collect3.default = ""
shost_Collect3.optional  = true

-- collectd_snmp_host.Collect4
shost_Collect4 = shost:option( Value, "Collect4", translate("Data Name4") )
shost_Collect4.default = ""
shost_Collect4.optional  = true


return m

then add these to nano /usr/lib/lua/luci/controller/luci_statistics/luci_statistics.lua

snmp            = _("SNMP"),

add to network section

   network = { "conntrack", "dns", "interface", "iptables", "snmp",
                    "netlink", "olsrd", "openvpn", "ping",
                    "splash_leases", "tcpconns", "iwinfo" }

lastly edit nano /etc/config/luci_statistics

 config statistics 'collectd_snmp'
   option enable '1'

you can use cd /usr/lib/lua/luci/statistics/rrdtool/definitions/
copy and renane interface.lua to snmp.lua and edit the top line to this

module("luci.statistics.rrdtool.definitions.snmp", package.seeall) to get quick funtionallity of snmp to add in more values toy need add them

Screenshot_20181230_110653

the luci statistics looks like this
Screenshot_20181230_111721

okay good luck have fun.

1 Like

okay here a sort a work around to get the items to order in the correct order to works some what reasonably okay-- important to remember add your all your host first and save then after that you can add in all the metrics you want . it will keep a proper order of data metric first then host info

--##################################--

function config_snmp( c )
 local str = ""


 for s in pairs(sections) do



     for key, type in pairs({ Sdata="collectd_snmp_data" }) do
             if sections[s][".type"] == type then

                     dname = sections[s].dname
                     Table = sections[s].Table
                     Type = sections[s].Type
                     Instance = sections[s].Instance
                     Values  = sections[s].Values
                     Values2 = sections[s].Values2
                     Values3 = sections[s].Values3
                     Values4 = sections[s].Values4
                            

                     if dname then
                             
                                     str = str .. "\t" .. "<Data " .."\"".. dname .."\"".." >" .. " \n"
                                     str = str .. "\t" .. "Type" .. "  " .. Type .. "\n"
                                     str = str .. "\t" .. "Table" .. " " .. Table .. "\n"
                                     str = str .. "\t" .. "Instance" .. " \"" .. Instance .. "\"\n"
                                    
                                     if Vaules4 then
                                                    str = str .. "\t" .. "Values" .. " \"" .. Values .. "\" \"" .. Values2 .. "\" \"" .. Values3 .. "\" \"" .. Values4 .. "\"\n"
                                          else
                                              if Values3 then
                                                            str = str .. "\t" .. "Values" .. " \"" .. Values .. "\" \"" .. Values2 .. "\" \"" .. Values3 .. "\"\n"         
                                               else
                                                   if Values2 then
                                                            str = str .. "\t" .. "Values" .. " \"" .. Values .. "\" \"" .. Values2 .. "\"\n"
                                                     else
                                                          str = str .. "\t" .. "Values" .. " \"" .. Values .. "\"\n"
                                                 
                                              end                                        
                                          end         
                                       end
                                      str = str .. "\t" .. "</Data>" .. " \n"
                      
                                  end                  
                              end
                      
     for key, type in pairs({  Shost="collectd_snmp_host" }) do
             if sections[s][".type"] == type then

                     hname = sections[s].hname
                     Address = sections[s].Address
                     Version = sections[s].Version
                     Interval = sections[s].Interval
                     Community = sections[s].Community
                     Timeout = sections[s].Timeout
                     Retries = sections[s].Retries
                     Collect = sections[s].Collect
                     Collect2 = sections[s].Collect2
                     Collect3 = sections[s].Collect3
                     Collect4 = sections[s].Collect4

                                
                                    
                            

                   if hname then            
                                    str = str .. "\t" .. "<Host " .."\"".. hname .."\"".." >" .. " \n"
                                    str = str .. "\t" .. "Address" .. " \"" .. Address .. "\"\n"
                                    str = str .. "\t" .. "Version" .. " " .. Version .. "\n"
                                    str = str .. "\t" .. "Community" .. " \"" .. Community .. "\"\n"
                                    str = str .. "\t" .. "Interval" .. " " .. Interval .. "\n"
                      
                                     if Collect4 then
                                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\" \"" .. Collect2 .. "\" \"" .. Collect3 .. "\" \"" .. Collect4 .. "\"\n"
                                          else
                                              if Collect3 then
                                                            str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\" \"" .. Collect2 .. "\" \"" .. Collect3 .. "\"\n"
                                               else
                                                   if Collect2 then
                                                            str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\"\"" .. Collect2 .. "\"\n"
                                                     else
                                                          str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\"\n"
                                                  
                                                   end
                                              end
                                       end
                           
                                    str = str .. "\t" .. "Timeout" .. " " .. Timeout .. "\n"
                                    str = str .. "\t" .. "Retries" .. " " .. Retries .. "\n"
                                    
                                    str = str .. "\t" .. "</Host>" .. " \n"
                               end
                           end 
                       end
                  end
              end
 return str

end


--#########################################--

Hm, I managed to get to create the luci config page (SNMP data), mini_snmp is running, but I do not see the output in statistics as shown above. What am I missing? I suppose some entry in a configfile . I want to monitor traffic ... I would appreciate any idea.
(Output of snmp testtool:

Paessler SNMP Tester - 20.1.58 Computername: 1130-ACER Interface: 192.168.102.99
04.06.2020 22:20:39 (11 ms) : Device: x.x.x.x
04.06.2020 22:20:39 (15 ms) : SNMP v1
04.06.2020 22:20:39 (19 ms) : 32Bit Counter
04.06.2020 22:20:39 (184 ms) : SNMP Datatype: ASN_COUNTER
04.06.2020 22:20:39 (195 ms) : -------
04.06.2020 22:20:39 (201 ms) : In: 4290649099
04.06.2020 22:20:39 (274 ms) : SNMP Datatype: ASN_COUNTER
04.06.2020 22:20:39 (292 ms) : Out: 1162581
04.06.2020 22:20:39 (307 ms) : Done

perhaps you do not have all the pacakages installed
collectd-mod-snmp
snmp-mibs
snmp-utils

No, are all installed:

root@OpenWrt236:~# opkg install collectd-mod-snmp
Package collectd-mod-snmp (5.8.1-1) installed in root is up to date.
root@OpenWrt236:~# opkg install snmp-mibs
Package snmp-mibs (5.7.3-10) installed in root is up to date.
root@OpenWrt236:~# opkg install snmp-utils
Package snmp-utils (5.7.3-10) installed in root is up to date.

Somehow the menu point "snmp" for Statistics is missing. In which configuration file this menue is located?

what version openwrt some others tried some of my other updates to +19 and they were not working.. i have not migrated to 19 yet as i am still running on ~18. so I have not really looked into the issue yet.. otherwise the menu loads under network plugins

Resuming work during lockdown :wink:
Just tried to install the module collectd-mod-snmp - 5.8.1-1
Graphs did not work either (when configuring via luci)
Did work after adding follwing plugin section to /etc/collectd.conf

LoadPlugin snmp
<Plugin snmp>
  <Data "if_Octets">
    Type "if_octets"
    Table true
    Values "IF-MIB::ifInOctets" "IF-MIB::ifOutOctets"
  </Data>
  <Host "xyz48">
    Address "x.x.x.x"
    Version 1
    Community "collectd"
    Collect "if_Octets"
    Interval 60
  </Host>
</Plugin>

hope it will survive reboot and editing with luci ...