MODBUS for Luci-Statistics

#1

for those who might be interested here is MODBUS for Luci-Statistic
it supports both TCP and RTU MODBUS connection types

nano /usr/bin/stat-genconfig



function config_modbus( c )
 local str = ""


 for s in pairs(sections) do



     for key, type in pairs({ Mdata="collectd_modbus_data", Mhost="collectd_modbus_host" }) do
             if sections[s][".type"] == type then

                     dname = sections[s].dname
                    RegisterType = sections[s].RegisterType
                    RegisterBase  = sections[s].RegisterBase
                     RegisterCmd = sections[s].RegisterCmd
                     Type = sections[s].Type
                     Instance = sections[s].Instance
                     Baudrate = sections[s].Baudrate
                     Device = sections[s].Device
                     Address = sections[s].Address
                     Port = sections[s].Port
                      Interval = sections[s].Interval
                     slname = sections[s].slname
                     hname = sections[s].hname
                     Instance2 = sections[s].Instance2
                     Collect = sections[s].Collect
                    Collect2 = sections[s].Collect2
                    Collect3 = sections[s].Collect3
                    Collect4 = sections[s].Collect4
   --                 Collect5 = sections[s].Collect5
        --             Collect6 = sections[s].Collect6
        --             Collect7 = sections[s].Collect7
          --           Collect8 = sections[s].Collect8
            --         Collect9 = sections[s].Collect9
              --       Collect10 = sections[s].Collect10

                    if dname then
                           if RegisterCmd  then
                                     str = str .. "\t" .. "<Data " .."\"".. dname .."\"".." >" .. " \n"
                                    str = str .. "\t" .. "RegisterBase" .. " " .. RegisterBase .. "\n"
                                     str = str .. "\t" .. "RegisterCmd" .. " " .. RegisterCmd .. "\n"
                                    str = str .. "\t" .. "RegisterType" .. " " .. RegisterType .. "\n"
                                    str = str .. "\t" .. "Type" .. " " .. Type .. "\n"
                                    str = str .. "\t" .. "Instance" .. " \"" .. Instance .. "\"\n"
                                    str = str .. "\t" .. "</Data>" .. " \n"
--
--
                            else
                                    str = str .. "\t" .. "<Data " .."\"".. dname .."\"".." >" .. " \n"
                                    str = str .. "\t" .. "RegisterBase" .. " " .. RegisterBase .. "\n"
                                    str = str .. "\t" .. "RegisterType" .. " " .. RegisterType .. "\n"
                                     str = str .. "\t" .. "Type" .. " " .. Type .. "\n"
                                    str = str .. "\t" .. "Instance" .. " \"" .. Instance .. "\"\n"
                                   str = str .. "\t" .. "</Data>" .. " \n"
--
end
                          end

if hname then
                          --      else
                                    str = str .. "\t" .. "<Host " .."\"".. hname .."\"".." >" .. " \n"
if Device then
                                   str = str .. "\t" .. "Device" .. " \"" .. Device .. "\"\n"
end
if Address then
                                    str = str .. "\t" .. "Address" .. " \"" .. Address .. "\"\n"
end
if Baudrate then
                                    str = str .. "\t" .. "Baudrate" .. " " .. Baudrate .. "\n"
end
if Port then                                   
                                    str = str .. "\t" .. "Port" .. " " .. Port .. "\n"
end                                  
                                    str = str .. "\t" .. "Interval" .. " " .. Interval .. "\n"
                                    str = str .. "\t" .. "<Slave " .."".. slname .."".." >" .. " \n"
if Instance2 then
                                    str = str .. "\t" .. "Instance" .. " \"" .. Instance2 .. "\"\n"
end                 
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\"\n"
if Collect2 then
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect2 .. "\"\n"
end
if Collect3 then
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect3 .. "\"\n"
end 
if Collect4 then
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect4 .. "\"\n"
end
-- if Collect5 then
  --                                 str = str .. "\t" .. "Collect" .. " \"" .. Collect5 .. "\"\n"
--  end
 --if Collect6 then                                 
   --                                str = str .. "\t" .. "Collect" .. " \"" .. Collect6 .. "\"\n"
-- end
 --if Collect7 then                                  
   --                                str = str .. "\t" .. "Collect" .. " \"" .. Collect7 .. "\"\n"
--end
 --if Collect8 then                                  
   ---                                str = str .. "\t" .. "Collect" .. " \"" .. Collect8 .. "\"\n"
 --end
 --if Collect9 then                                  
   --                                str = str .. "\t" .. "Collect" .. " \"" .. Collect9 .. "\"\n"
 --end
 --if Collect10 then                                  
 --                                  str = str .. "\t" .. "Collect" .. " \"" .. Collect10 .. "\"\n"
  --end                                  
                                   str = str .. "\t" .. "</Slave>" .. " \n"
                                     str = str .. "\t" .. "</Host>" .. " \n"

                             end
                  
                 end

                     end
     end
 return str

end



and add this to plugin section

  modbus = config_modbus,

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

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

m = Map("luci_statistics",
        translate("Modbus Data"),
        translate(
                "MODBUS DATA" 
                
        ))



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

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


-- collectd_modbus_data config section (Mdata)
mdata = m:section( TypedSection, "collectd_modbus_data",
        translate("Modbus Data"),
        translate(
                "This section defines the Modbus Data " 
               
        ))
mdata.addremove = true
mdata.anonymous = true


-- collectd_modbus_data.dname
mdata_name = mdata:option( Value, "dname", translate("Modbus Data Name"))
mdata_name.default = "Data1"


-- collectd_modbus_data.RegisterBase
mdata_RegisterBase = mdata:option( Value, "RegisterBase", translate("RegisterBase"))
mdata_RegisterBase.default = "1234"


-- collectd_modbus_data.RegisterCmd
mdata_RegisterCmd = mdata:option( Value, "RegisterCmd", translate("RegisterCmd") )
---mdata_port.isinteger = true
mdata_RegisterCmd.default   = ""
mdata_RegisterCmd.optional  = true


-- collectd_modbus_data.RegisterType
mdata_RegisterType = mdata:option( Value, "RegisterType", translate("RegisterType") )
mdata_RegisterType.isinteger = true
mdata_RegisterType.default   = "float"


-- collectd_modbus_data.Type
mdata_Type = mdata:option( Value, "Type", translate("Type") )
--mdata_Type.isinteger = true
mdata_Type.default   = "voltage"


-- collectd_modbus_data.Instance
mdata_Instance = mdata:option( Value, "Instance", translate("Instance") )
--mdata_Instance.isinteger = true
mdata_Instance.default   = "..."


-- collectd_modbus_host config section (Mhost)
mhost = m:section( TypedSection, "collectd_modbus_host",
        translate("Modbus Hosts"),
        translate(
                "This section defines to Modbus Hosts "..
                " sellect Address/Port or Device/Baudrate" 
                
        ))
mhost.addremove = true
mhost.anonymous = true

-- collectd_modbus_host.hname
mhost_name = mhost:option( Value, "hname", translate("Modbus Host Name"))
mhost_name.default = "Name"


-- collectd_modbus_host.Address
mhost_Address = mhost:option( Value, "Address", translate("TCP - Host Address") )
mhost_Address.default = ""
mhost_Address.optional  = true

-- collectd_modbus_host.Port
mhost_Port = mhost:option( Value, "Port", translate("Port") )
mhost_Port.default   = ""
mhost_Port.isinteger = true
mhost_Port.optional  = true

-- collectd_modbus_host.Device
mhost_Device = mhost:option( Value, "Device", translate("RTU DEVICE - /dev/ttyUSB0") )
mhost_Device.default = ""
mhost_Device.optional  = true

-- collectd_modbus_host.Baudrate
mhost_Baudrate = mhost:option( Value, "Baudrate", translate("Baudrate") )
mhost_Baudrate.default   = ""
mhost_Baudrate.isinteger = true
mhost_Baudrate.optional  = true

-- collectd_modbus_host.Interval
mhost_Interval = mhost:option( Value, "Interval", translate("Interval") )
mhost_Interval.default = "60"
mhost_Interval.isinteger = true


mhost_name = mhost:option( Value, "slname", translate("Modbus Slave Number"))
mhost_name.default = "1"

-- collectd_modbus_slave.Instance
mhost_Instance2 = mhost:option( Value, "Instance2", translate("Instance") )
mhost_Instance2.default = ""
mhost_Instance2.isinteger = true
mhost_Instance2.optional  = true


-- collectd_modbus_slave.Collect
mhost_Collect = mhost:option( Value, "Collect", translate("Data Name") )
mhost_Collect.default = "data_name"

-- collectd_modbus_slave.Collect2
mhost_Collect2 = mhost:option( Value, "Collect2", translate("Data Name2") )
mhost_Collect2.default = ""
mhost_Collect2.isinteger = true
mhost_Collect2.optional  = true

-- collectd_modbus_slave.Collect3
mhost_Collect3 = mhost:option( Value, "Collect3", translate("Data Name3") )
mhost_Collect3.default = ""
mhost_Collect3.isinteger = true
mhost_Collect3.optional  = true

-- collectd_modbus_slave.Collect4
mhost_Collect4 = mhost:option( Value, "Collect4", translate("Data Name4") )
mhost_Collect4.default = ""
mhost_Collect4.isinteger = true
mhost_Collect4.optional  = true

-- collectd_modbus_slave.Collect5
--mhost_Collect5 = mhost:option( Value, "Collect5", translate("Data Name5") )
--mhost_Collect5.default = ""
--mhost_Collect5.isinteger = true
--mhost_Collect5.optional  = true

-- collectd_modbus_slave.Collect6
--mhost_Collect6 = mhost:option( Value, "Collect6", translate("Data Name6") )
--mhost_Collect6.default = ""
--mhost_Collect6.isinteger = true
--mhost_Collect6.optional  = true

-- collectd_modbus_slave.Collect7
--mhost_Collect7 = mhost:option( Value, "Collect7", translate("Data Name7") )
--mhost_Collect7.default = ""
--mhost_Collect7.isinteger = true
--mhost_Collect7.optional  = true

-- collectd_modbus_slave.Collect8
--mhost_Collect8 = mhost:option( Value, "Collect8", translate("Data Name8") )
--mhost_Collect8.default = ""
--mhost_Collect8.isinteger = true
--mhost_Collect8.optional  = true

-- collectd_modbus_slave.Collect9
--mhost_Collect9 = mhost:option( Value, "Collect9", translate("Data Name9") )
--mhost_Collect9.default = ""
--mhost_Collect9.isinteger = true
--mhost_Collect9.optional  = true

-- collectd_modbus_slave.Collect10
--mhost_Collect10 = mhost:option( Value, "Collect10", translate("Data Name 10") )
--mhost_Collect10.default = ""
--mhost_Collect10.isinteger = true
--mhost_Collect10.optional  = true

return m

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

modbus            = _("MODBUS"),

add to network section

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

lastly edit nano /etc/config/luci_statistics

 config statistics 'collectd_modbus'
   option enable '1'

the Luci interface looks like this

Screenshot_20181230_111649

you have to create your own rrd deffinitions as I have not done that at the moment

0 Likes

#2

here an update to modbus to keep collectd in proper order - first set up hosts first save and then add metric afterwards it will maintain a proper sequence..

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


function config_modbus( c )
 local str = ""


 for s in pairs(sections) do



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

                     dname = sections[s].dname
                     RegisterType = sections[s].RegisterType
                     RegisterBase  = sections[s].RegisterBase
                     RegisterCmd = sections[s].RegisterCmd
                     Type = sections[s].Type
                     Instance = sections[s].Instance
                    

                    if dname then
                           if RegisterCmd  then
                                    str = str .. "\t" .. "<Data " .."\"".. dname .."\"".." >" .. " \n"
                                    str = str .. "\t" .. "RegisterBase" .. " " .. RegisterBase .. "\n"
                                    str = str .. "\t" .. "RegisterCmd" .. " " .. RegisterCmd .. "\n"
                                    str = str .. "\t" .. "RegisterType" .. " " .. RegisterType .. "\n"
                                    str = str .. "\t" .. "Type" .. " " .. Type .. "\n"
                                    str = str .. "\t" .. "Instance" .. " \"" .. Instance .. "\"\n"
                                    str = str .. "\t" .. "</Data>" .. " \n"
--
--
                            else
                                    str = str .. "\t" .. "<Data " .."\"".. dname .."\"".." >" .. " \n"
                                    str = str .. "\t" .. "RegisterBase" .. " " .. RegisterBase .. "\n"
                                    str = str .. "\t" .. "RegisterType" .. " " .. RegisterType .. "\n"
                                    str = str .. "\t" .. "Type" .. " " .. Type .. "\n"
                                    str = str .. "\t" .. "Instance" .. " \"" .. Instance .. "\"\n"
                                    str = str .. "\t" .. "</Data>" .. " \n"
--
end
                          end
end
end
     for key, type in pairs({ Mhost="collectd_modbus_host" }) do
             if sections[s][".type"] == type then

                     Baudrate = sections[s].Baudrate
                     Device = sections[s].Device
                     Address = sections[s].Address
                     Port = sections[s].Port
                     Interval = sections[s].Interval
                     slname = sections[s].slname
                     hname = sections[s].hname
                     Instance2 = sections[s].Instance2
                     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"
if Device then
                                   str = str .. "\t" .. "Device" .. " \"" .. Device .. "\"\n"
end
if Address then
                                    str = str .. "\t" .. "Address" .. " \"" .. Address .. "\"\n"
end
if Baudrate then
                                    str = str .. "\t" .. "Baudrate" .. " " .. Baudrate .. "\n"
end
if Port then                                   
                                    str = str .. "\t" .. "Port" .. " " .. Port .. "\n"
end                                  
                                    str = str .. "\t" .. "Interval" .. " " .. Interval .. "\n"
                                    str = str .. "\t" .. "<Slave " .."".. slname .."".." >" .. " \n"
if Instance2 then
                                    str = str .. "\t" .. "Instance" .. " \"" .. Instance2 .. "\"\n"
end                 
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect .. "\"\n"
if Collect2 then
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect2 .. "\"\n"
end
if Collect3 then
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect3 .. "\"\n"
end 
if Collect4 then
                                    str = str .. "\t" .. "Collect" .. " \"" .. Collect4 .. "\"\n"
end
                                 
                                   str = str .. "\t" .. "</Slave>" .. " \n"
                                     str = str .. "\t" .. "</Host>" .. " \n"

                             end
                        end
                    end                 
                 end

 return str

end



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

0 Likes