Example #1
0
// watchSystemdService() attempts to determine the main PID of a service and
// builds a WatchedProc{} which is then sent to linuxProcMonitor()
func watchSystemdServiceProc(md *opentsdb.MultiDataPoint, conn *dbus.Conn, unit dbus.UnitStatus) error {
	// ExecMainPID can be non-running. MainPID is the pid of the running service.
	mainPIDProp, err := conn.GetUnitTypeProperty(unit.Name, "Service", "MainPID")
	if err != nil {
		return err
	}

	mainPID := mainPIDProp.Value.Value().(uint32)
	// MainPID is 0 if there is no running service.
	if mainPID == 0 {
		return nil
	}

	cmdline, err := getLinuxCmdline(fmt.Sprint(mainPID))
	if err != nil {
		return err
	}
	if cmdline == nil {
		return nil
	}

	wp := WatchedProc{
		Command:   regexp.MustCompile("^" + regexp.QuoteMeta(cmdline[0]) + "$"),
		Name:      strings.TrimSuffix(unit.Name, ".service"),
		Processes: make(map[string]int),
		ArgMatch:  regexp.MustCompile(""),
		idPool:    new(idPool)}

	// Since we only have one PID per service (at the moment), this is always set to 1
	wp.Processes[fmt.Sprint(mainPID)] = wp.get()

	if e := linuxProcMonitor(&wp, md); e != nil {
		return e
	}

	return err
}