Skip to content

Commit

Permalink
⭐️ support aix services
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Sep 26, 2023
1 parent 2882ef3 commit b5793bb
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
66 changes: 66 additions & 0 deletions providers/os/resources/services/aixlssrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package services

import (
"bufio"
"go.mondoo.com/cnquery/providers/os/connection/shared"
"io"
"regexp"
)

type AixServiceManager struct {
conn shared.Connection
}

func (s *AixServiceManager) Name() string {
return "System Resource Controller"
}

func (s *AixServiceManager) List() ([]*Service, error) {
cmd, err := s.conn.RunCommand("lssrc -a")
if err != nil {
return nil, err
}

entries := parseLssrc(cmd.Stdout)
services := make([]*Service, len(entries))
for i, entry := range entries {
services[i] = &Service{
Name: entry.Subsystem,
Enabled: entry.Status == "active",
Installed: true,
Running: entry.Status == "active",
Type: "aix",
}
}
return services, nil
}

type lssrcEntry struct {
Subsystem string
Group string
PID string
Status string
}

var lssrcRegex = regexp.MustCompile(`^\s([\w.-]+)(\s+[\w]+\s+){0,1}([\d]+){0,1}\s+([\w]+)$`)

func parseLssrc(input io.Reader) []lssrcEntry {
entries := []lssrcEntry{}
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
m := lssrcRegex.FindStringSubmatch(line)
if len(m) == 5 {
entries = append(entries, lssrcEntry{
Subsystem: m[1],
Group: m[2],
PID: m[3],
Status: m[4],
})
}
}
return entries
}
35 changes: 35 additions & 0 deletions providers/os/resources/services/aixlssrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package services

import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestLssrcParse(t *testing.T) {
testOutput := `
Subsystem Group PID Status
syslogd ras 3932558 active
aso 4653462 active
biod nfs 5046692 active
rpc.lockd nfs 5636560 active
qdaemon spooler 5767630 active
ctrmc rsct 5439966 active
pmperfrec 6881768 active
IBM.HostRM rsct_rm 5898530 active
automountd autofs 7340402 active
lpd spooler inoperative
nimsh nimclient inoperative
nimhttp inoperative
timed tcpip inoperative
`
entries := parseLssrc(strings.NewReader(testOutput))
assert.Equal(t, 13, len(entries), "detected the right amount of services")
assert.Equal(t, "syslogd", entries[0].Subsystem, "service name detected")
assert.Equal(t, "active", entries[0].Status, "service status detected")
assert.Equal(t, "timed", entries[12].Subsystem, "service name detected")
assert.Equal(t, "inoperative", entries[12].Status, "service status detected")
}
2 changes: 2 additions & 0 deletions providers/os/resources/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func ResolveManager(conn shared.Connection) (OSServiceManager, error) {
osm = &AlpineOpenrcServiceManager{conn: conn}
case asset.Platform.Name == "cos":
osm = ResolveSystemdServiceManager(conn)
case asset.Platform.Name == "aix":
osm = &AixServiceManager{conn: conn}
case asset.Platform.Name == "kali": // debian based with versions from 2015 onwards being systemd based
osm = ResolveSystemdServiceManager(conn)
}
Expand Down

0 comments on commit b5793bb

Please sign in to comment.