diff options
author | Eriks Sneiders <eriks.sneiders@zabbix.com> | 2022-02-03 17:23:30 +0300 |
---|---|---|
committer | Eriks Sneiders <eriks.sneiders@zabbix.com> | 2022-02-18 10:49:19 +0300 |
commit | 76c76ab26928ae0b9dbf121fc88c9a30120c4f75 (patch) | |
tree | 4f88aa972d44ad8c08cedce91a39f5c2d1df39be /src/go/plugins | |
parent | 3b0e7514a79b8b3843e971bc09b888fa94db609f (diff) |
...G...... [ZBX-20030] fixed system.cpu.num runtime cpu change in Zabbix agent 2
* commit '1bd747756705f228be7dab888be3c198d9414a64':
...G...... [ZBX-20030] code clean up
.......... [ZBX-20030] fixed linux discovery
.......... [ZBX-20030] aded change-log
...G...... [ZBX-20030] code clean up
...G...... [ZBX-20030] code clean up
...G...... [ZBX-20030] fixed system.cpu.num runtime cpu change count for windows in Zabbix agent 2
...G...... [ZBX-20030] fixed system.cpu.num runtime cpu change count for linux in Zabbix agent 2
(cherry picked from commit c2dfb47315478463670b65e7a2b9af1fc3f3dcec)
Diffstat (limited to 'src/go/plugins')
-rw-r--r-- | src/go/plugins/system/cpu/cpu.go | 18 | ||||
-rw-r--r-- | src/go/plugins/system/cpu/cpu_linux.go | 29 | ||||
-rw-r--r-- | src/go/plugins/system/cpu/cpu_linux_test.go | 136 | ||||
-rw-r--r-- | src/go/plugins/system/cpu/cpu_windows.go | 9 |
4 files changed, 177 insertions, 15 deletions
diff --git a/src/go/plugins/system/cpu/cpu.go b/src/go/plugins/system/cpu/cpu.go index e2eb6e9e86c..5f22df69edf 100644 --- a/src/go/plugins/system/cpu/cpu.go +++ b/src/go/plugins/system/cpu/cpu.go @@ -23,6 +23,8 @@ import ( "encoding/json" "errors" "strconv" + + "zabbix.com/pkg/zbxerr" ) const pluginName = "Cpu" @@ -101,29 +103,21 @@ func (p *Plugin) getCpuDiscovery(params []string) (result interface{}, err error } func (p *Plugin) getCpuNum(params []string) (result interface{}, err error) { - mask := cpuStatusOnline switch len(params) { case 1: switch params[0] { case "", "online": - // default value, already initialized + return numCPUOnline(), nil case "max": - mask = cpuStatusOnline | cpuStatusOffline + return numCPUConf(), nil default: return nil, errors.New("Invalid first parameter.") } case 0: + return numCPUOnline(), nil default: - return nil, errors.New("Too many parameters.") - } - - var num int - for i := 1; i < len(p.cpus); i++ { - if p.cpus[i].status&mask != 0 { - num++ - } + return nil, zbxerr.ErrorTooManyParameters } - return num, nil } func periodByMode(mode string) (period historyIndex) { diff --git a/src/go/plugins/system/cpu/cpu_linux.go b/src/go/plugins/system/cpu/cpu_linux.go index e9eb21e8695..d52fff06fd4 100644 --- a/src/go/plugins/system/cpu/cpu_linux.go +++ b/src/go/plugins/system/cpu/cpu_linux.go @@ -77,11 +77,14 @@ func (p *Plugin) Collect() (err error) { if i, err = strconv.ParseInt(fields[0][3:], 10, 32); err != nil { return } - if index = int(i); index < 0 || index+1 >= len(p.cpus) { + + if index = int(i); index < 0 { p.Debugf("invalid CPU index %d", index) continue } + p.addCpu(index) + status = cpuStatusOnline } else { index = -1 @@ -112,12 +115,32 @@ func (p *Plugin) Collect() (err error) { return nil } -func numCPU() int { +func (p *Plugin) addCpu(index int) { + if p == nil || p.cpus == nil { + return + } + + if index == 0 { + return + } + + if index+1 >= len(p.cpus) { + for idx := p.cpus[len(p.cpus)-1].index; idx < index; idx++ { + p.cpus = append(p.cpus, &cpuUnit{index: idx + 1, status: cpuStatusOffline}) + } + } +} + +func numCPUConf() int { return int(C.sysconf(C._SC_NPROCESSORS_CONF)) } +func numCPUOnline() int { + return int(C.sysconf(C._SC_NPROCESSORS_ONLN)) +} + func (p *Plugin) Start() { - p.cpus = p.newCpus(numCPU()) + p.cpus = p.newCpus(numCPUConf()) } func (p *Plugin) Stop() { diff --git a/src/go/plugins/system/cpu/cpu_linux_test.go b/src/go/plugins/system/cpu/cpu_linux_test.go new file mode 100644 index 00000000000..87d719352ca --- /dev/null +++ b/src/go/plugins/system/cpu/cpu_linux_test.go @@ -0,0 +1,136 @@ +/* +** Zabbix +** Copyright (C) 2001-2022 Zabbix SIA +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**/ + +package cpu + +import ( + "reflect" + "testing" +) + +func TestPlugin_addCpu(t *testing.T) { + type fields struct { + cpus []*cpuUnit + } + type args struct { + index int + } + tests := []struct { + name string + fields fields + args args + want []*cpuUnit + }{ + { + "one_offline_cpu", + fields{ + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + }, + }, + args{2}, + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + {index: 2, status: cpuStatusOffline}, + }, + }, + { + "two_offline_cpu", + fields{ + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + }, + }, + args{3}, + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + {index: 2, status: cpuStatusOffline}, + {index: 3, status: cpuStatusOffline}, + }, + }, + { + "ten_offline_cpu", + fields{ + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + }, + }, + args{11}, + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + {index: 2, status: cpuStatusOffline}, + {index: 3, status: cpuStatusOffline}, + {index: 4, status: cpuStatusOffline}, + {index: 5, status: cpuStatusOffline}, + {index: 6, status: cpuStatusOffline}, + {index: 7, status: cpuStatusOffline}, + {index: 8, status: cpuStatusOffline}, + {index: 9, status: cpuStatusOffline}, + {index: 10, status: cpuStatusOffline}, + {index: 11, status: cpuStatusOffline}, + }, + }, + { + "no_offline_cpu", + fields{ + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + }, + }, + args{1}, + []*cpuUnit{ + {index: -1, status: cpuStatusOffline}, + {index: 0, status: cpuStatusOffline}, + {index: 1, status: cpuStatusOffline}, + }, + }, + { + "empty", fields{[]*cpuUnit{}}, args{}, []*cpuUnit{}, + }, + { + "nil", fields{nil}, args{}, nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &Plugin{ + cpus: tt.fields.cpus, + } + p.addCpu(tt.args.index) + + if !reflect.DeepEqual(p.cpus, tt.want) { + t.Errorf("addCpu() got = %v, want %v", p.cpus, tt.want) + } + }) + } +} diff --git a/src/go/plugins/system/cpu/cpu_windows.go b/src/go/plugins/system/cpu/cpu_windows.go index 0b175a17dfe..eff02894286 100644 --- a/src/go/plugins/system/cpu/cpu_windows.go +++ b/src/go/plugins/system/cpu/cpu_windows.go @@ -43,6 +43,15 @@ const ( defaultIndex = 60 ) +func numCPUOnline() int { + return numCPU() +} + +func numCPUConf() int { + // unsupported on Windows + return 0 +} + func numCPU() (numCpu int) { size, err := win32.GetLogicalProcessorInformationEx(win32.RelationProcessorCore, nil) if err != nil { |