1// Licensed to Elasticsearch B.V. under one or more contributor
2// license agreements. See the NOTICE file distributed with
3// this work for additional information regarding copyright
4// ownership. Elasticsearch B.V. licenses this file to you under
5// the Apache License, Version 2.0 (the "License"); you may
6// not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package types
19
20import "time"
21
22// Process is the main wrapper for gathering information on a process
23type Process interface {
24	CPUTimer
25	Info() (ProcessInfo, error)
26	Memory() (MemoryInfo, error)
27	User() (UserInfo, error)
28	Parent() (Process, error)
29	PID() int
30}
31
32// ProcessInfo contains basic stats about a process
33type ProcessInfo struct {
34	Name      string    `json:"name"`
35	PID       int       `json:"pid"`
36	PPID      int       `json:"ppid"`
37	CWD       string    `json:"cwd"`
38	Exe       string    `json:"exe"`
39	Args      []string  `json:"args"`
40	StartTime time.Time `json:"start_time"`
41}
42
43// UserInfo contains information about the UID and GID
44// values of a process.
45type UserInfo struct {
46	// UID is the user ID.
47	// On Linux and Darwin (macOS) this is the real user ID.
48	// On Windows, this is the security identifier (SID) of the
49	// user account of the process access token.
50	UID string `json:"uid"`
51
52	// On Linux and Darwin (macOS) this is the effective user ID.
53	// On Windows, this is empty.
54	EUID string `json:"euid"`
55
56	// On Linux and Darwin (macOS) this is the saved user ID.
57	// On Windows, this is empty.
58	SUID string `json:"suid"`
59
60	// GID is the primary group ID.
61	// On Linux and Darwin (macOS) this is the real group ID.
62	// On Windows, this is the security identifier (SID) of the
63	// primary group of the process access token.
64	GID string `json:"gid"`
65
66	// On Linux and Darwin (macOS) this is the effective group ID.
67	// On Windows, this is empty.
68	EGID string `json:"egid"`
69
70	// On Linux and Darwin (macOS) this is the saved group ID.
71	// On Windows, this is empty.
72	SGID string `json:"sgid"`
73}
74
75// Environment is the interface that wraps the Environment method.
76// Environment returns variables for a process
77type Environment interface {
78	Environment() (map[string]string, error)
79}
80
81// OpenHandleEnumerator is the interface that wraps the OpenHandles method.
82// OpenHandles lists the open file handles.
83type OpenHandleEnumerator interface {
84	OpenHandles() ([]string, error)
85}
86
87// OpenHandleCounter is the interface that wraps the OpenHandleCount method.
88// OpenHandleCount returns the number of open file handles.
89type OpenHandleCounter interface {
90	OpenHandleCount() (int, error)
91}
92
93// CPUTimer is the interface that wraps the CPUTime method.
94// CPUTime returns CPU time info
95type CPUTimer interface {
96	// CPUTime returns a CPUTimes structure for
97	// the host or some process.
98	//
99	// The User and System fields are guaranteed
100	// to be populated for all platforms, and
101	// for both hosts and processes.
102	CPUTime() (CPUTimes, error)
103}
104
105// CPUTimes contains CPU timing stats for a process
106type CPUTimes struct {
107	User    time.Duration `json:"user"`
108	System  time.Duration `json:"system"`
109	Idle    time.Duration `json:"idle,omitempty"`
110	IOWait  time.Duration `json:"iowait,omitempty"`
111	IRQ     time.Duration `json:"irq,omitempty"`
112	Nice    time.Duration `json:"nice,omitempty"`
113	SoftIRQ time.Duration `json:"soft_irq,omitempty"`
114	Steal   time.Duration `json:"steal,omitempty"`
115}
116
117// Total returns the total CPU time
118func (cpu CPUTimes) Total() time.Duration {
119	return cpu.User + cpu.System + cpu.Idle + cpu.IOWait + cpu.IRQ + cpu.Nice +
120		cpu.SoftIRQ + cpu.Steal
121}
122
123// MemoryInfo contains memory stats for a process
124type MemoryInfo struct {
125	Resident uint64            `json:"resident_bytes"`
126	Virtual  uint64            `json:"virtual_bytes"`
127	Metrics  map[string]uint64 `json:"raw,omitempty"` // Other memory related metrics.
128}
129
130// SeccompInfo contains seccomp info for a process
131type SeccompInfo struct {
132	Mode       string `json:"mode"`
133	NoNewPrivs *bool  `json:"no_new_privs,omitempty"` // Added in kernel 4.10.
134}
135
136// CapabilityInfo contains capability set info.
137type CapabilityInfo struct {
138	Inheritable []string `json:"inheritable"`
139	Permitted   []string `json:"permitted"`
140	Effective   []string `json:"effective"`
141	Bounding    []string `json:"bounding"`
142	Ambient     []string `json:"ambient"`
143}
144
145// Capabilities is the interface that wraps the Capabilities method.
146// Capabilities returns capabilities for a process
147type Capabilities interface {
148	Capabilities() (*CapabilityInfo, error)
149}
150
151// Seccomp is the interface that wraps the Seccomp method.
152// Seccomp returns seccomp info on Linux
153type Seccomp interface {
154	Seccomp() (*SeccompInfo, error)
155}
156