Skip to content

Commit 0e3b1f5

Browse files
authored
Merge pull request #100 from coroot/systemd_triggered_by
add `systemd_triggered_by` label to `container_info` metric to detect periodic jobs triggered by systemd timers
2 parents 4787e13 + d0e515d commit 0e3b1f5

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

containers/container.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ type ContainerNetwork struct {
3535
}
3636

3737
type ContainerMetadata struct {
38-
name string
39-
labels map[string]string
40-
volumes map[string]string
41-
logPath string
42-
image string
43-
logDecoder logparser.Decoder
44-
hostListens map[string][]netaddr.IPPort
45-
networks map[string]ContainerNetwork
46-
env map[string]string
38+
name string
39+
labels map[string]string
40+
volumes map[string]string
41+
logPath string
42+
image string
43+
logDecoder logparser.Decoder
44+
hostListens map[string][]netaddr.IPPort
45+
networks map[string]ContainerNetwork
46+
env map[string]string
47+
systemdTriggeredBy string
4748
}
4849

4950
type Delays struct {
@@ -225,8 +226,8 @@ func (c *Container) Collect(ch chan<- prometheus.Metric) {
225226
c.lock.RLock()
226227
defer c.lock.RUnlock()
227228

228-
if c.metadata.image != "" {
229-
ch <- gauge(metrics.ContainerInfo, 1, c.metadata.image)
229+
if c.metadata.image != "" || c.metadata.systemdTriggeredBy != "" {
230+
ch <- gauge(metrics.ContainerInfo, 1, c.metadata.image, c.metadata.systemdTriggeredBy)
230231
}
231232

232233
ch <- counter(metrics.Restarts, float64(c.restarts))

containers/metrics.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var metrics = struct {
4747
JvmSafepointSyncTime *prometheus.Desc
4848
Ip2Fqdn *prometheus.Desc
4949
}{
50-
ContainerInfo: metric("container_info", "Meta information about the container", "image"),
50+
ContainerInfo: metric("container_info", "Meta information about the container", "image", "systemd_triggered_by"),
5151

5252
Restarts: metric("container_restarts_total", "Number of times the container was restarted"),
5353

containers/registry.go

+4
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID {
410410

411411
func getContainerMetadata(cg *cgroup.Cgroup) (*ContainerMetadata, error) {
412412
switch cg.ContainerType {
413+
case cgroup.ContainerTypeSystemdService:
414+
md := &ContainerMetadata{}
415+
md.systemdTriggeredBy = SystemdTriggeredBy(cg.ContainerId)
416+
return md, nil
413417
case cgroup.ContainerTypeDocker, cgroup.ContainerTypeContainerd, cgroup.ContainerTypeSandbox, cgroup.ContainerTypeCrio:
414418
default:
415419
return &ContainerMetadata{}, nil

containers/systemd.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package containers
2+
3+
import (
4+
"context"
5+
"os"
6+
"strconv"
7+
"strings"
8+
"time"
9+
10+
"github.com/coroot/coroot-node-agent/proc"
11+
12+
"github.com/coreos/go-systemd/v22/dbus"
13+
gdbus "github.com/godbus/dbus/v5"
14+
15+
"k8s.io/klog/v2"
16+
)
17+
18+
var (
19+
conn *dbus.Conn
20+
dbusTimeout = time.Second
21+
)
22+
23+
func init() {
24+
var err error
25+
conn, err = dbus.NewConnection(func() (*gdbus.Conn, error) {
26+
c, err := gdbus.Dial("unix:path=" + proc.HostPath("/run/systemd/private"))
27+
if err != nil {
28+
return nil, err
29+
}
30+
methods := []gdbus.Auth{gdbus.AuthExternal(strconv.Itoa(os.Getuid()))}
31+
if err = c.Auth(methods); err != nil {
32+
conn.Close()
33+
return nil, err
34+
}
35+
return c, nil
36+
})
37+
if err != nil {
38+
klog.Warningln("failed to connect to systemd bus:", err)
39+
}
40+
}
41+
42+
func SystemdTriggeredBy(id string) string {
43+
if conn == nil {
44+
return ""
45+
}
46+
ctx, cancel := context.WithTimeout(context.Background(), dbusTimeout)
47+
defer cancel()
48+
parts := strings.Split(id, "/")
49+
unit := parts[len(parts)-1]
50+
if prop, _ := conn.GetUnitPropertyContext(ctx, unit, "TriggeredBy"); prop != nil {
51+
if values, _ := prop.Value.Value().([]string); len(values) > 0 {
52+
return values[0]
53+
}
54+
}
55+
return ""
56+
}

0 commit comments

Comments
 (0)