Skip to content

Commit 2b99aca

Browse files
Add function to get sys services (minio#20)
The intention is to provide status of any sys services that can potentially impact the performance of minio. At present, it will return information about the `selinux` service (not-installed/disabled/permissive/enforcing)
1 parent c6633ca commit 2b99aca

File tree

1 file changed

+79
-10
lines changed

1 file changed

+79
-10
lines changed

health.go

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ const (
5353
SysErrUpdatedbInstalled = "updatedb is installed"
5454
)
5555

56+
const (
57+
SrvSELinux = "selinux"
58+
SrvNotInstalled = "not-installed"
59+
)
60+
5661
// NodeInfo - Interface to abstract any struct that contains address/endpoint and error fields
5762
type NodeInfo interface {
5863
GetAddr() string
5964
SetAddr(addr string)
65+
SetError(err string)
6066
}
6167

6268
// NodeCommon - Common fields across most node-specific health structs
@@ -75,13 +81,31 @@ func (n *NodeCommon) SetAddr(addr string) {
7581
n.Addr = addr
7682
}
7783

84+
// SetError - set the address of the node
85+
func (n *NodeCommon) SetError(err string) {
86+
n.Error = err
87+
}
88+
7889
// SysErrors - contains a system error
7990
type SysErrors struct {
8091
NodeCommon
8192

8293
Errors []string `json:"errors,omitempty"`
8394
}
8495

96+
// SysServices - info about services that affect minio
97+
type SysServices struct {
98+
NodeCommon
99+
100+
Services []SysService `json:"services,omitempty"`
101+
}
102+
103+
// SysService - name and status of a sys service
104+
type SysService struct {
105+
Name string `json:"name"`
106+
Status string `json:"status"`
107+
}
108+
85109
// CPU contains system's CPU information.
86110
type CPU struct {
87111
VendorID string `json:"vendor_id"`
@@ -267,7 +291,48 @@ func GetOSInfo(ctx context.Context, addr string) OSInfo {
267291
return osInfo
268292
}
269293

270-
// GetSysErrors returns system's RAM and swap information.
294+
// GetSysServices returns info of sys services that affect minio
295+
func GetSysServices(ctx context.Context, addr string) SysServices {
296+
ss := SysServices{
297+
NodeCommon: NodeCommon{Addr: addr},
298+
Services: []SysService{},
299+
}
300+
srv, e := getSELinuxInfo()
301+
if e != nil {
302+
ss.Error = e.Error()
303+
} else {
304+
ss.Services = append(ss.Services, srv)
305+
}
306+
307+
return ss
308+
}
309+
310+
func getSELinuxInfo() (SysService, error) {
311+
ss := SysService{Name: SrvSELinux}
312+
313+
file, err := os.Open("/etc/selinux/config")
314+
if err != nil {
315+
if errors.Is(err, os.ErrNotExist) {
316+
ss.Status = SrvNotInstalled
317+
return ss, nil
318+
}
319+
return ss, err
320+
}
321+
defer file.Close()
322+
323+
scanner := bufio.NewScanner(file)
324+
for scanner.Scan() {
325+
tokens := strings.SplitN(strings.TrimSpace(scanner.Text()), "=", 2)
326+
if len(tokens) == 2 && tokens[0] == "SELINUX" {
327+
ss.Status = tokens[1]
328+
return ss, nil
329+
}
330+
}
331+
332+
return ss, scanner.Err()
333+
}
334+
335+
// GetSysErrors returns issues in system setup/config
271336
func GetSysErrors(ctx context.Context, addr string) SysErrors {
272337
se := SysErrors{NodeCommon: NodeCommon{Addr: addr}}
273338
if runtime.GOOS != "linux" {
@@ -282,15 +347,15 @@ func GetSysErrors(ctx context.Context, addr string) SysErrors {
282347
}
283348

284349
_, err = exec.LookPath("updatedb")
285-
if err != nil {
350+
if err == nil {
351+
se.Errors = append(se.Errors, SysErrUpdatedbInstalled)
352+
} else if !strings.HasSuffix(err.Error(), exec.ErrNotFound.Error()) {
286353
errMsg := "updatedb: " + err.Error()
287354
if len(se.Error) == 0 {
288355
se.Error = errMsg
289356
} else {
290357
se.Error = se.Error + ", " + errMsg
291358
}
292-
} else {
293-
se.Errors = append(se.Errors, SysErrUpdatedbInstalled)
294359
}
295360

296361
return se
@@ -584,12 +649,13 @@ func GetProcInfo(ctx context.Context, addr string) ProcInfo {
584649

585650
// SysInfo - Includes hardware and system information of the MinIO cluster
586651
type SysInfo struct {
587-
CPUInfo []CPUs `json:"cpus,omitempty"`
588-
Partitions []Partitions `json:"partitions,omitempty"`
589-
OSInfo []OSInfo `json:"osinfo,omitempty"`
590-
MemInfo []MemInfo `json:"meminfo,omitempty"`
591-
ProcInfo []ProcInfo `json:"procinfo,omitempty"`
592-
SysErrs []SysErrors `json:"errors,omitempty"`
652+
CPUInfo []CPUs `json:"cpus,omitempty"`
653+
Partitions []Partitions `json:"partitions,omitempty"`
654+
OSInfo []OSInfo `json:"osinfo,omitempty"`
655+
MemInfo []MemInfo `json:"meminfo,omitempty"`
656+
ProcInfo []ProcInfo `json:"procinfo,omitempty"`
657+
SysErrs []SysErrors `json:"errors,omitempty"`
658+
SysServices []SysServices `json:"services,omitempty"`
593659
}
594660

595661
// Latency contains write operation latency in seconds of a disk drive.
@@ -767,6 +833,7 @@ const (
767833
HealthDataTypeSysNet HealthDataType = "sysnet"
768834
HealthDataTypeSysProcess HealthDataType = "sysprocess"
769835
HealthDataTypeSysErrors HealthDataType = "syserrors"
836+
HealthDataTypeSysServices HealthDataType = "sysservices"
770837
)
771838

772839
// HealthDataTypesMap - Map of Health datatypes
@@ -784,6 +851,7 @@ var HealthDataTypesMap = map[string]HealthDataType{
784851
"sysnet": HealthDataTypeSysNet,
785852
"sysprocess": HealthDataTypeSysProcess,
786853
"syserrors": HealthDataTypeSysErrors,
854+
"sysservices": HealthDataTypeSysServices,
787855
}
788856

789857
// HealthDataTypesList - List of Health datatypes
@@ -801,6 +869,7 @@ var HealthDataTypesList = []HealthDataType{
801869
HealthDataTypeSysNet,
802870
HealthDataTypeSysProcess,
803871
HealthDataTypeSysErrors,
872+
HealthDataTypeSysServices,
804873
}
805874

806875
// HealthInfoVersionStruct - struct for health info version

0 commit comments

Comments
 (0)