Skip to content

Commit f3b599c

Browse files
committed
collect host systemctl services
1 parent 810b3cb commit f3b599c

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ type Certificate struct {
8888
KeyPath string `json:"keyPath" yaml:"keyPath"`
8989
}
9090

91+
type HostServices struct {
92+
HostCollectorMeta `json:",inline" yaml:",inline"`
93+
}
94+
9195
type HostCollect struct {
9296
CPU *CPU `json:"cpu,omitempty" yaml:"cpu,omitempty"`
9397
Memory *Memory `json:"memory,omitempty" yaml:"memory,omitempty"`
@@ -103,6 +107,7 @@ type HostCollect struct {
103107
TCPConnect *TCPConnect `json:"tcpConnect,omitempty" yaml:"tcpConnect,omitempty"`
104108
FilesystemPerformance *FilesystemPerformance `json:"filesystemPerformance,omitempty" yaml:"filesystemPerformance,omitempty"`
105109
Certificate *Certificate `json:"certificate,omitempty" yaml:"certificate,omitempty"`
110+
HostServices *HostServices `json:"hostServices,omitempty" yaml:"hostServices,omitempty"`
106111
}
107112

108113
func (c *HostCollect) GetName() string {

pkg/collect/host_collector.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func GetHostCollector(collector *troubleshootv1beta2.HostCollect) (HostCollector
3838
return &CollectHostFilesystemPerformance{collector.FilesystemPerformance}, true
3939
case collector.Certificate != nil:
4040
return &CollectHostCertificate{collector.Certificate}, true
41+
case collector.HostServices != nil:
42+
return &CollectHostServices{collector.HostServices}, true
4143
default:
4244
return nil, false
4345
}

pkg/collect/host_services.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package collect
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/json"
7+
"fmt"
8+
"os/exec"
9+
10+
"github.com/pkg/errors"
11+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
12+
)
13+
14+
type ServiceInfo struct {
15+
Unit string `json:"Unit"`
16+
Load string `json:"Load"`
17+
Active string `json:"Active"`
18+
Sub string `json:"Sub"`
19+
}
20+
21+
const systemctlFormat = `%s %s %s %s` // this leaves off the description
22+
23+
type CollectHostServices struct {
24+
hostCollector *troubleshootv1beta2.HostServices
25+
}
26+
27+
func (c *CollectHostServices) Title() string {
28+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Block Devices")
29+
}
30+
31+
func (c *CollectHostServices) IsExcluded() (bool, error) {
32+
return isExcluded(c.hostCollector.Exclude)
33+
}
34+
35+
func (c *CollectHostServices) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
36+
var devices []ServiceInfo
37+
38+
cmd := exec.Command("systemctl", "list-units", "--type=service", "--no-legend", "--all")
39+
stdout, err := cmd.Output()
40+
if err != nil {
41+
return nil, errors.Wrapf(err, "failed to execute systemctl")
42+
}
43+
buf := bytes.NewBuffer(stdout)
44+
scanner := bufio.NewScanner(buf)
45+
46+
for scanner.Scan() {
47+
bdi := ServiceInfo{}
48+
fmt.Sscanf(
49+
scanner.Text(),
50+
systemctlFormat,
51+
&bdi.Unit,
52+
&bdi.Load,
53+
&bdi.Active,
54+
&bdi.Sub,
55+
)
56+
57+
devices = append(devices, bdi)
58+
}
59+
60+
b, err := json.Marshal(devices)
61+
if err != nil {
62+
return nil, errors.Wrap(err, "failed to marshal systemctl service info")
63+
}
64+
65+
return map[string][]byte{
66+
"system/systemctl_services.json": b,
67+
}, nil
68+
}

0 commit comments

Comments
 (0)