// c_systemd() iterates through all units provided by systemd's dbus info. // If the unit is a service we care about, it sends service metrics. // Also sends process data if WatchProc was set to true in the [[SystemdService]] config. func c_systemd() (opentsdb.MultiDataPoint, error) { conn, err := dbus.New() if err != nil { return nil, err } defer conn.Close() units, err := conn.ListUnits() if err != nil { return nil, err } var md opentsdb.MultiDataPoint for _, unit := range units { if strings.HasSuffix(unit.Name, ".service") { shortName := strings.TrimSuffix(unit.Name, ".service") for _, config := range systemdServices { if config.regex.MatchString(shortName) { if config.watch { err = watchSystemdServiceProc(&md, conn, unit) if err != nil { return nil, err } } if _, ok := activeState[unit.ActiveState]; !ok { return nil, fmt.Errorf("error: unit %s has unknown ActiveState: %s\n", unit.Name, unit.ActiveState) } systemdTags := opentsdb.TagSet{"name": unit.Name} osTags := opentsdb.TagSet{"name": shortName} Add(&md, "linux.systemd.unit.activestate", activeState[unit.ActiveState], systemdTags, metadata.Gauge, metadata.StatusCode, descActiveState) Add(&md, osServiceRunning, util.Btoi(unit.ActiveState == "active"), osTags, metadata.Gauge, metadata.Bool, osServiceRunningDesc) } } } } return md, err }
func c_windows_processes() (opentsdb.MultiDataPoint, error) { var dst []Win32_PerfRawData_PerfProc_Process var q = wmi.CreateQuery(&dst, `WHERE Name <> '_Total'`) err := queryWmi(q, &dst) if err != nil { return nil, err } var svc_dst []Win32_Service var svc_q = wmi.CreateQuery(&svc_dst, "") err = queryWmi(svc_q, &svc_dst) if err != nil { return nil, err } var iis_dst []WorkerProcess iis_q := wmi.CreateQuery(&iis_dst, "") err = queryWmiNamespace(iis_q, &iis_dst, "root\\WebAdministration") if err != nil { // Don't return from this error since the name space might exist. iis_dst = nil } var numberOfLogicalProcessors uint64 var core_dst []Win32_ComputerSystem var core_q = wmi.CreateQuery(&core_dst, "") err = queryWmi(core_q, &core_dst) if err != nil { return nil, err } for _, y := range core_dst { numberOfLogicalProcessors = uint64(y.NumberOfLogicalProcessors) } if numberOfLogicalProcessors == 0 { return nil, fmt.Errorf("invalid result: numberOfLogicalProcessors=%v", numberOfLogicalProcessors) } var md opentsdb.MultiDataPoint var svc_dst_started []Win32_Service for _, svc := range svc_dst { if util.NameMatches(svc.Name, regexesProcesses) { if svc.Started { svc_dst_started = append(svc_dst_started, svc) } tags := opentsdb.TagSet{"name": svc.Name} Add(&md, "win.service.started", util.Btoi(svc.Started), tags, metadata.Gauge, metadata.Bool, descWinServiceStarted) Add(&md, "win.service.status", util.Btoi(svc.Status != "OK"), tags, metadata.Gauge, metadata.Ok, descWinServiceStatus) Add(&md, "win.service.checkpoint", svc.CheckPoint, tags, metadata.Gauge, metadata.None, descWinServiceCheckPoint) Add(&md, "win.service.wait_hint", svc.WaitHint, tags, metadata.Gauge, metadata.MilliSecond, descWinServiceWaitHint) Add(&md, osServiceRunning, util.Btoi(svc.Started), tags, metadata.Gauge, metadata.Bool, osServiceRunningDesc) } } totalCPUByName := make(map[string]uint64) totalVirtualMemByName := make(map[string]uint64) totalPrivateWSMemByName := make(map[string]uint64) countByName := make(map[string]int) for _, v := range dst { var name string service_match := false iis_match := false process_match := util.NameMatches(v.Name, regexesProcesses) id := "0" if process_match { raw_name := strings.Split(v.Name, "#") name = raw_name[0] if len(raw_name) == 2 { id = raw_name[1] } // If you have a hash sign in your process name you don't deserve monitoring ;-) if len(raw_name) > 2 { continue } } // A Service match could "overwrite" a process match, but that is probably what we would want for _, svc := range svc_dst_started { // It is possible the pid has gone and been reused, but I think this unlikely // And I'm not aware of an atomic join we could do anyways if svc.ProcessId != 0 && svc.ProcessId == v.IDProcess { id = "0" service_match = true name = svc.Name break } } for _, a_pool := range iis_dst { if a_pool.ProcessId == v.IDProcess { id = "0" iis_match = true name = strings.Join([]string{"iis", a_pool.AppPoolName}, "_") break } } if v.IDProcess == uint32(os.Getpid()) { TotalScollectorMemoryMB = v.WorkingSetPrivate / 1024 / 1024 } if !(service_match || process_match || iis_match) { continue } //Use timestamp from WMI to fix issues with CPU metrics ts := TSys100NStoEpoch(v.Timestamp_Sys100NS) tags := opentsdb.TagSet{"name": name, "id": id} AddTS(&md, "win.proc.cpu", ts, v.PercentPrivilegedTime/NS100_Seconds/numberOfLogicalProcessors, opentsdb.TagSet{"type": "privileged"}.Merge(tags), metadata.Counter, metadata.Pct, descWinProcCPU_priv) AddTS(&md, "win.proc.cpu", ts, v.PercentUserTime/NS100_Seconds/numberOfLogicalProcessors, opentsdb.TagSet{"type": "user"}.Merge(tags), metadata.Counter, metadata.Pct, descWinProcCPU_user) totalCPUByName[name] += v.PercentUserTime / NS100_Seconds / numberOfLogicalProcessors AddTS(&md, "win.proc.cpu_total", ts, v.PercentProcessorTime/NS100_Seconds/numberOfLogicalProcessors, tags, metadata.Counter, metadata.Pct, descWinProcCPU_total) if v.Frequency_Object != 0 { Add(&md, "win.proc.elapsed_time", (v.Timestamp_Object-v.ElapsedTime)/v.Frequency_Object, tags, metadata.Gauge, metadata.Second, descWinProcElapsed_time) } Add(&md, "win.proc.handle_count", v.HandleCount, tags, metadata.Gauge, metadata.Count, descWinProcHandle_count) Add(&md, "win.proc.io_bytes", v.IOOtherBytesPersec, opentsdb.TagSet{"type": "other"}.Merge(tags), metadata.Counter, metadata.BytesPerSecond, descWinProcIo_bytes_other) Add(&md, "win.proc.io_bytes", v.IOReadBytesPersec, opentsdb.TagSet{"type": "read"}.Merge(tags), metadata.Counter, metadata.BytesPerSecond, descWinProcIo_bytes_read) Add(&md, "win.proc.io_bytes", v.IOWriteBytesPersec, opentsdb.TagSet{"type": "write"}.Merge(tags), metadata.Counter, metadata.BytesPerSecond, descWinProcIo_bytes_write) Add(&md, "win.proc.io_operations", v.IOOtherOperationsPersec, opentsdb.TagSet{"type": "other"}.Merge(tags), metadata.Counter, metadata.Operation, descWinProcIo_operations) Add(&md, "win.proc.io_operations", v.IOReadOperationsPersec, opentsdb.TagSet{"type": "read"}.Merge(tags), metadata.Counter, metadata.Operation, descWinProcIo_operations_read) Add(&md, "win.proc.io_operations", v.IOWriteOperationsPersec, opentsdb.TagSet{"type": "write"}.Merge(tags), metadata.Counter, metadata.Operation, descWinProcIo_operations_write) Add(&md, "win.proc.mem.page_faults", v.PageFaultsPersec, tags, metadata.Counter, metadata.PerSecond, descWinProcMemPage_faults) Add(&md, "win.proc.mem.pagefile_bytes", v.PageFileBytes, tags, metadata.Gauge, metadata.Bytes, descWinProcMemPagefile_bytes) Add(&md, "win.proc.mem.pagefile_bytes_peak", v.PageFileBytesPeak, tags, metadata.Gauge, metadata.Bytes, descWinProcMemPagefile_bytes_peak) Add(&md, "win.proc.mem.pool_nonpaged_bytes", v.PoolNonpagedBytes, tags, metadata.Gauge, metadata.Bytes, descWinProcMemPool_nonpaged_bytes) Add(&md, "win.proc.mem.pool_paged_bytes", v.PoolPagedBytes, tags, metadata.Gauge, metadata.Bytes, descWinProcMemPool_paged_bytes) Add(&md, "win.proc.mem.vm.bytes", v.VirtualBytes, tags, metadata.Gauge, metadata.Bytes, descWinProcMemVmBytes) totalVirtualMemByName[name] += v.VirtualBytes Add(&md, "win.proc.mem.vm.bytes_peak", v.VirtualBytesPeak, tags, metadata.Gauge, metadata.Bytes, descWinProcMemVmBytes_peak) Add(&md, "win.proc.mem.working_set", v.WorkingSet, tags, metadata.Gauge, metadata.Bytes, descWinProcMemWorking_set) Add(&md, "win.proc.mem.working_set_peak", v.WorkingSetPeak, tags, metadata.Gauge, metadata.Bytes, descWinProcMemWorking_set_peak) Add(&md, "win.proc.mem.working_set_private", v.WorkingSetPrivate, tags, metadata.Gauge, metadata.Bytes, descWinProcMemWorking_set_private) totalPrivateWSMemByName[name] += v.WorkingSetPrivate Add(&md, "win.proc.priority_base", v.PriorityBase, tags, metadata.Gauge, metadata.None, descWinProcPriority_base) Add(&md, "win.proc.private_bytes", v.PrivateBytes, tags, metadata.Gauge, metadata.Bytes, descWinProcPrivate_bytes) Add(&md, "win.proc.thread_count", v.ThreadCount, tags, metadata.Gauge, metadata.Count, descWinProcthread_count) Add(&md, "win.proc.pid", v.IDProcess, tags, metadata.Gauge, metadata.Unit("PID"), osProcPID) countByName[name]++ } for name, count := range countByName { if count < 1 { continue } Add(&md, osProcCount, count, opentsdb.TagSet{"name": name}, metadata.Gauge, metadata.Process, osProcCountDesc) if totalCPU, ok := totalCPUByName[name]; ok { Add(&md, osProcCPU, totalCPU, opentsdb.TagSet{"name": name}, metadata.Counter, metadata.Pct, osProcCPUDesc) } if totalVM, ok := totalVirtualMemByName[name]; ok { Add(&md, osProcMemVirtual, totalVM, opentsdb.TagSet{"name": name}, metadata.Gauge, metadata.Bytes, osProcMemVirtualDesc) } if totalPWS, ok := totalPrivateWSMemByName[name]; ok { Add(&md, osProcMemReal, totalPWS, opentsdb.TagSet{"name": name}, metadata.Gauge, metadata.Bytes, osProcMemRealDesc) } } return md, nil }