1/* 2** Zabbix 3** Copyright (C) 2001-2021 Zabbix SIA 4** 5** This program is free software; you can redistribute it and/or modify 6** it under the terms of the GNU General Public License as published by 7** the Free Software Foundation; either version 2 of the License, or 8** (at your option) any later version. 9** 10** This program is distributed in the hope that it will be useful, 11** but WITHOUT ANY WARRANTY; without even the implied warranty of 12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13** GNU General Public License for more details. 14** 15** You should have received a copy of the GNU General Public License 16** along with this program; if not, write to the Free Software 17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18**/ 19 20package cpu 21 22const ( 23 counterUnknown cpuCounter = iota - 1 24 counterUtil 25 counterLoad 26) 27 28const minimumSampleCount = 2 29 30type cpuCounters struct { 31 load float64 32 util float64 33} 34 35func counterByType(name string) (counter cpuCounter) { 36 switch name { 37 case "", "system": 38 return counterUtil 39 default: 40 return counterUnknown 41 } 42} 43 44func (c *cpuUnit) counterAverage(counter cpuCounter, period historyIndex, split int) (value interface{}) { 45 if c.head == c.tail { 46 return 47 } 48 var tail, head *cpuCounters 49 totalnum := c.tail - c.head 50 if totalnum < 0 { 51 totalnum += maxHistory 52 } 53 if totalnum < minimumSampleCount { 54 // need at least two samples to calculate change over period 55 return 56 } 57 58 if totalnum-1 < period { 59 period = totalnum - 1 60 } 61 tail = &c.history[c.tail.dec()] 62 head = &c.history[c.tail.sub(period+1)] 63 64 switch counter { 65 case counterUtil: 66 return getCounterUtil(tail, head, period) 67 case counterLoad: 68 return getCounterLoad(tail, head, period, split) 69 case counterUnknown: 70 return 71 } 72 73 return 74} 75 76func getCounterUtil(tail, head *cpuCounters, period historyIndex) interface{} { 77 if tail.util < head.util { 78 return nil 79 } 80 81 return (tail.util - head.util) / float64(period) 82} 83 84func getCounterLoad(tail, head *cpuCounters, period historyIndex, split int) interface{} { 85 if tail.load < head.load { 86 return nil 87 } 88 89 return (tail.load - head.load) / float64(split) / float64(period) 90} 91