Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
omnistat authored Mar 11, 2024
2 parents 09dadd9 + bbb0902 commit e3e6cd7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,49 @@ format.
The `--config.file` parameter can be used multiple times to load more than one file.
It also supports [glob filename matching](https://pkg.go.dev/path/filepath#Glob), e.g. `modbus_*.yml`.

## Systemd service

You can create a systemd service if you want to run modbus exporter as a service in the background. Start by creating a modbus_exporter system account (example on Debian)

```shell
useradd -r modbus_exporter
```

Place the modbus_exporter binary at `/usr/local/bin/modbus_exporter`

The following example systemd unit file can be saved to `/etc/systemd/system/modbus_exporter.service`:

```systemd
[Unit]
Description=Modbus TCP Prometheus exporter
Requires=network.target
After=network-online.target
Wants=network-online.target
[Service]
User=modbus_exporter
Group=nogroup
ExecStart=/usr/local/bin/modbus_exporter --config.file='/etc/modbus_exporter.yml'
Restart=on-failure
RestartSec=1
[Install]
WantedBy=multi-user.target
```

Then locate your config file in `/etc/modbus_exporter.yml`, and run the following commands to make systemd aware of config changes and startup the modbus_exporter

```shell
systemctl daemon-reload
systemctl start modbus_exporter
```

In order to start the service at boot, run the following

```shell
systemctl enable modbus_exporter
```

## TODO

- Rework logging.
Expand Down
22 changes: 14 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package config

import (
"fmt"
"time"

multierror "github.com/hashicorp/go-multierror"
)
Expand Down Expand Up @@ -59,14 +60,19 @@ type ListTargets map[string]*Module

// Module defines the configuration parameters of a modbus module.
type Module struct {
Name string `yaml:"name"`
Protocol ModbusProtocol `yaml:"protocol"`
Timeout int `yaml:"timeout"`
Baudrate int `yaml:"baudrate"`
Databits int `yaml:"databits"`
Stopbits int `yaml:"stopbits"`
Parity string `yaml:"parity"`
Metrics []MetricDef `yaml:"metrics"`
Name string `yaml:"name"`
Protocol ModbusProtocol `yaml:"protocol"`
Timeout int `yaml:"timeout"`
Baudrate int `yaml:"baudrate"`
Databits int `yaml:"databits"`
Stopbits int `yaml:"stopbits"`
Parity string `yaml:"parity"`
Metrics []MetricDef `yaml:"metrics"`
Workarounds Workarounds `yaml:"workarounds"`
}

type Workarounds struct {
SleepAfterConnect time.Duration `yaml:"sleepAfterConnect"`
}

// RegisterAddr specifies the register in the possible output of _digital
Expand Down
4 changes: 4 additions & 0 deletions modbus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ modules:
# Module name, needs to be passed as parameter by Prometheus.
- name: "fake"
protocol: 'tcp/ip'
# Certain modbus devices need special timing workarounds
workarounds:
# Sleep a certain time after the TCP connection is established
sleepAfterConnect: "1s"
metrics:
# Name of the metric.
- name: "power_consumption_total"
Expand Down
4 changes: 4 additions & 0 deletions modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (e *Exporter) Scrape(targetAddress string, subTarget byte, moduleName strin
targetAddress, module.Name)
}

if module.Workarounds.SleepAfterConnect > 0 {
time.Sleep(module.Workarounds.SleepAfterConnect)
}

// TODO: Should we reuse this?
c := modbus.NewClient(handler)

Expand Down

0 comments on commit e3e6cd7

Please sign in to comment.