mirror of
https://codeberg.org/frosty/modbot.git
synced 2024-09-19 11:46:34 -04:00
make each reader func return a func, as well as add an exec reader.
This commit is contained in:
parent
781b804a33
commit
4653550119
21
config.go
21
config.go
|
@ -30,16 +30,23 @@ var (
|
||||||
|
|
||||||
var modules = []Module{
|
var modules = []Module{
|
||||||
{
|
{
|
||||||
Func: readers.ReadUptime,
|
Func: readers.ReadExec("statusbar cpu"),
|
||||||
|
Interval: 5 * time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Func: readers.ReadExec("statusbar volume"),
|
||||||
|
Signal: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Func: readers.ReadExec("statusbar battery"),
|
||||||
|
Interval: 60 * time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Func: readers.ReadExec("statusbar date"),
|
||||||
Interval: 1 * time.Second,
|
Interval: 1 * time.Second,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Func: readers.ReadLoad,
|
Func: readers.ReadExec("statusbar loadavg"),
|
||||||
Interval: 5 * time.Second,
|
Interval: 5 * time.Second,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Func: readers.ReadMemory,
|
|
||||||
Interval: 5 * time.Second,
|
|
||||||
Template: "{{.UsedPretty}} / {{.TotalPretty}}",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,7 @@ func BatteryTechnologyFromStr(technologyStr string) BatteryTechnology {
|
||||||
return TechnologyUnknown
|
return TechnologyUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadBattery(batteryName string) (interface{}, error) {
|
func readBattery(batteryName string) (interface{}, error) {
|
||||||
capacityPath := fmt.Sprintf("/sys/class/power_supply/%s/capacity", batteryName)
|
capacityPath := fmt.Sprintf("/sys/class/power_supply/%s/capacity", batteryName)
|
||||||
statusPath := fmt.Sprintf("/sys/class/power_supply/%s/status", batteryName)
|
statusPath := fmt.Sprintf("/sys/class/power_supply/%s/status", batteryName)
|
||||||
technologyPath := fmt.Sprintf("/sys/class/power_supply/%s/technology", batteryName)
|
technologyPath := fmt.Sprintf("/sys/class/power_supply/%s/technology", batteryName)
|
||||||
|
@ -169,3 +169,9 @@ func ReadBattery(batteryName string) (interface{}, error) {
|
||||||
Technology: BatteryTechnologyFromStr(batteryTechnology),
|
Technology: BatteryTechnologyFromStr(batteryTechnology),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadBattery(batteryName string) func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readBattery(batteryName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ import (
|
||||||
|
|
||||||
type CpuTemperatureInfo float32
|
type CpuTemperatureInfo float32
|
||||||
|
|
||||||
func ReadCpuTemperature(hwmonName, tempName string) (interface{}, error) {
|
func readCpuTemperature(hwmonName, tempName string) (interface{}, error) {
|
||||||
tempPath := fmt.Sprintf("/sys/class/hwmon/%s/%s_input", hwmonName, tempName)
|
tempPath := fmt.Sprintf("/sys/class/hwmon/%s/%s_input", hwmonName, tempName)
|
||||||
|
|
||||||
file, err := os.Open(tempPath)
|
file, err := os.Open(tempPath)
|
||||||
|
@ -49,3 +49,9 @@ func ReadCpuTemperature(hwmonName, tempName string) (interface{}, error) {
|
||||||
|
|
||||||
return CpuTemperatureInfo(cpuTemperature), nil
|
return CpuTemperatureInfo(cpuTemperature), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadCpuTemperature(hwmonName, tempName string) func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readCpuTemperature(hwmonName, tempName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func (cu CpuUsageInfo) String() string {
|
||||||
return fmt.Sprintf("%d%%", uint(cu.UsagePercent))
|
return fmt.Sprintf("%d%%", uint(cu.UsagePercent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadCpuUsage() (interface{}, error) {
|
func readCpuUsage() (interface{}, error) {
|
||||||
file, err := os.Open("/proc/stat")
|
file, err := os.Open("/proc/stat")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return CpuUsageInfo{}, err
|
return CpuUsageInfo{}, err
|
||||||
|
@ -81,3 +81,9 @@ func ReadCpuUsage() (interface{}, error) {
|
||||||
UsagePercent: float64(cpuInUse) * 100 / float64(cpuTotal),
|
UsagePercent: float64(cpuInUse) * 100 / float64(cpuTotal),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadCpuUsage() func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readCpuUsage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
53
lib/readers/exec.go
Normal file
53
lib/readers/exec.go
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
// modbot is a system information agregator
|
||||||
|
// Copyright (C) 2024 frosty <inthishouseofcards@gmail.com>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published
|
||||||
|
// by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package readers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExecInfo string
|
||||||
|
|
||||||
|
func readExec(command string) (interface{}, error) {
|
||||||
|
args := []string{"sh", "-c", command}
|
||||||
|
var stdout, stderr bytes.Buffer
|
||||||
|
|
||||||
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return ExecInfo(""), err
|
||||||
|
}
|
||||||
|
|
||||||
|
outputLines := strings.Split(stdout.String(), "\n")
|
||||||
|
if len(outputLines) == 0 {
|
||||||
|
return ExecInfo(""), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
outputString := outputLines[0]
|
||||||
|
return ExecInfo(outputString), nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadExec(command string) func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readExec(command)
|
||||||
|
}
|
||||||
|
}
|
|
@ -33,7 +33,7 @@ func (l LoadInfo) String() string {
|
||||||
return fmt.Sprintf("%v %v %v", l.OneMinute, l.FiveMinute, l.FifteenMinute)
|
return fmt.Sprintf("%v %v %v", l.OneMinute, l.FiveMinute, l.FifteenMinute)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadLoad() (interface{}, error) {
|
func readLoad() (interface{}, error) {
|
||||||
const loadPath = "/proc/loadavg"
|
const loadPath = "/proc/loadavg"
|
||||||
|
|
||||||
file, err := os.Open(loadPath)
|
file, err := os.Open(loadPath)
|
||||||
|
@ -60,3 +60,9 @@ func ReadLoad() (interface{}, error) {
|
||||||
FifteenMinute: fields[2],
|
FifteenMinute: fields[2],
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadLoad() func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readLoad()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ type MemoryInfo struct {
|
||||||
UsedPretty string
|
UsedPretty string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadMemory() (interface{}, error) {
|
func readMemory() (interface{}, error) {
|
||||||
file, err := os.Open("/proc/meminfo")
|
file, err := os.Open("/proc/meminfo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MemoryInfo{}, err
|
return MemoryInfo{}, err
|
||||||
|
@ -89,3 +89,9 @@ func ReadMemory() (interface{}, error) {
|
||||||
UsedPretty: ui.PrettifyKib(memUsed, 2),
|
UsedPretty: ui.PrettifyKib(memUsed, 2),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadMemory() func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readMemory()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ type OsInfo struct {
|
||||||
Version string
|
Version string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadOs() (interface{}, error) {
|
func readOs() (interface{}, error) {
|
||||||
const osPath = "/lib/os-release"
|
const osPath = "/lib/os-release"
|
||||||
|
|
||||||
file, err := os.Open(osPath)
|
file, err := os.Open(osPath)
|
||||||
|
@ -70,3 +70,9 @@ func ReadOs() (interface{}, error) {
|
||||||
Version: osVersion,
|
Version: osVersion,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadOs() func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readOs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ func (u UptimeInfo) String() string {
|
||||||
return builder.String()
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadUptime() (interface{}, error) {
|
func readUptime() (interface{}, error) {
|
||||||
const uptimePath = "/proc/uptime"
|
const uptimePath = "/proc/uptime"
|
||||||
|
|
||||||
file, err := os.Open(uptimePath)
|
file, err := os.Open(uptimePath)
|
||||||
|
@ -92,3 +92,9 @@ func ReadUptime() (interface{}, error) {
|
||||||
|
|
||||||
return UptimeInfo(uptime), nil
|
return UptimeInfo(uptime), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadUptime() func() (interface{}, error) {
|
||||||
|
return func() (interface{}, error) {
|
||||||
|
return readUptime()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue