Add battery info
This commit is contained in:
@@ -1 +1,62 @@
|
|||||||
package collectors
|
package collectors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func batteryCapacity() (capacity int) {
|
||||||
|
const filename string = "/sys/class/power_supply/BAT0/capacity"
|
||||||
|
var rawCapacity string
|
||||||
|
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
|
if scanner.Scan() {
|
||||||
|
rawCapacity = scanner.Text()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
capacity, err = strconv.Atoi(rawCapacity)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return capacity
|
||||||
|
}
|
||||||
|
|
||||||
|
func batteryStatus() (status string) {
|
||||||
|
const filename string = "/sys/class/power_supply/BAT0/status"
|
||||||
|
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
|
if scanner.Scan() {
|
||||||
|
status = scanner.Text()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
func batteryInfo() (capacity int, status string) {
|
||||||
|
const statusFile string = "/sys/class/power_supply/BAT0/status"
|
||||||
|
capacity = batteryCapacity()
|
||||||
|
status = batteryStatus()
|
||||||
|
return capacity, status
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type StatusInfo struct {
|
|||||||
func Collect() (*StatusInfo, error) {
|
func Collect() (*StatusInfo, error) {
|
||||||
year, month, day, hour, minute, second := datetime()
|
year, month, day, hour, minute, second := datetime()
|
||||||
cpuLoad1, cpuLoad5, cpuLoad15 := cpuLoad()
|
cpuLoad1, cpuLoad5, cpuLoad15 := cpuLoad()
|
||||||
|
capacity, status := batteryInfo()
|
||||||
return &StatusInfo{
|
return &StatusInfo{
|
||||||
CPULoad1: cpuLoad1,
|
CPULoad1: cpuLoad1,
|
||||||
CPULoad5: cpuLoad5,
|
CPULoad5: cpuLoad5,
|
||||||
@@ -27,7 +28,7 @@ func Collect() (*StatusInfo, error) {
|
|||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
BatteryPercent: 100,
|
BatteryPercent: capacity,
|
||||||
BatteryStatus: "Charging",
|
BatteryStatus: status,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import (
|
|||||||
|
|
||||||
func Print(c *collectors.StatusInfo) {
|
func Print(c *collectors.StatusInfo) {
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"LOAD: %.2f %.2f %.2f | %d-%02d-%02d %02d:%02d\n",
|
"BAT: %d%% (%s) | LOAD: %.2f %.2f %.2f | %d-%02d-%02d %02d:%02d\n",
|
||||||
|
c.BatteryPercent,
|
||||||
|
c.BatteryStatus,
|
||||||
c.CPULoad1,
|
c.CPULoad1,
|
||||||
c.CPULoad5,
|
c.CPULoad5,
|
||||||
c.CPULoad15,
|
c.CPULoad15,
|
||||||
|
|||||||
Reference in New Issue
Block a user