github.com/netdata/go.d.plugin@v0.58.1/modules/windows/collect_process.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package windows
     4  
     5  import (
     6  	"strings"
     7  
     8  	"github.com/netdata/go.d.plugin/pkg/prometheus"
     9  )
    10  
    11  const (
    12  	metricProcessCPUTimeTotal    = "windows_process_cpu_time_total"
    13  	metricProcessWorkingSetBytes = "windows_process_working_set_private_bytes"
    14  	metricProcessIOBytes         = "windows_process_io_bytes_total"
    15  	metricProcessIOOperations    = "windows_process_io_operations_total"
    16  	metricProcessPageFaults      = "windows_process_page_faults_total"
    17  	metricProcessPageFileBytes   = "windows_process_page_file_bytes"
    18  	metricProcessThreads         = "windows_process_threads"
    19  	metricProcessCPUHandles      = "windows_process_handles"
    20  )
    21  
    22  func (w *Windows) collectProcess(mx map[string]int64, pms prometheus.Series) {
    23  	if !w.cache.collection[collectorProcess] {
    24  		w.cache.collection[collectorProcess] = true
    25  		w.addProcessesCharts()
    26  	}
    27  
    28  	seen := make(map[string]bool)
    29  	px := "process_"
    30  	for _, pm := range pms.FindByName(metricProcessCPUTimeTotal) {
    31  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    32  			seen[name] = true
    33  			mx[px+name+"_cpu_time"] += int64(pm.Value * 1000)
    34  		}
    35  	}
    36  	for _, pm := range pms.FindByName(metricProcessWorkingSetBytes) {
    37  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    38  			seen[name] = true
    39  			mx[px+name+"_working_set_private_bytes"] += int64(pm.Value)
    40  		}
    41  	}
    42  	for _, pm := range pms.FindByName(metricProcessIOBytes) {
    43  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    44  			seen[name] = true
    45  			mx[px+name+"_io_bytes"] += int64(pm.Value)
    46  		}
    47  	}
    48  	for _, pm := range pms.FindByName(metricProcessIOOperations) {
    49  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    50  			seen[name] = true
    51  			mx[px+name+"_io_operations"] += int64(pm.Value)
    52  		}
    53  	}
    54  	for _, pm := range pms.FindByName(metricProcessPageFaults) {
    55  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    56  			seen[name] = true
    57  			mx[px+name+"_page_faults"] += int64(pm.Value)
    58  		}
    59  	}
    60  	for _, pm := range pms.FindByName(metricProcessPageFileBytes) {
    61  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    62  			seen[name] = true
    63  			mx[px+name+"_page_file_bytes"] += int64(pm.Value)
    64  		}
    65  	}
    66  	for _, pm := range pms.FindByName(metricProcessThreads) {
    67  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    68  			seen[name] = true
    69  			mx[px+name+"_threads"] += int64(pm.Value)
    70  		}
    71  	}
    72  	for _, pm := range pms.FindByName(metricProcessCPUHandles) {
    73  		if name := cleanProcessName(pm.Labels.Get("process")); name != "" {
    74  			seen[name] = true
    75  			mx[px+name+"_handles"] += int64(pm.Value)
    76  		}
    77  	}
    78  
    79  	for proc := range seen {
    80  		if !w.cache.processes[proc] {
    81  			w.cache.processes[proc] = true
    82  			w.addProcessToCharts(proc)
    83  		}
    84  	}
    85  	for proc := range w.cache.processes {
    86  		if !seen[proc] {
    87  			delete(w.cache.processes, proc)
    88  			w.removeProcessFromCharts(proc)
    89  		}
    90  	}
    91  }
    92  
    93  func cleanProcessName(name string) string {
    94  	return strings.ReplaceAll(name, " ", "_")
    95  }