2626 if multiple counters are fetched during one call with this function if the sleep
2727 is done afterwards manually. A sleep is set to 500ms to ensure counter data is
2828 valid and contains an offset from previous/current values
29+ . PARAMETER NoCache
30+ Set this if no caching of the counter is intended. This will prevent from adding
31+ single counters to the internal cache during update phase
2932. INPUTS
3033 System.String
3134. LINK
@@ -36,7 +39,8 @@ function New-IcingaPerformanceCounter()
3639{
3740 param (
3841 [string ]$Counter = ' ' ,
39- [boolean ]$SkipWait = $FALSE
42+ [boolean ]$SkipWait = $FALSE ,
43+ [switch ]$NoCache = $FALSE
4044 );
4145
4246 # Simply use the counter name, like
@@ -45,7 +49,7 @@ function New-IcingaPerformanceCounter()
4549 return (New-IcingaPerformanceCounterNullObject - FullName $Counter - ErrorMessage ' Failed to initialise counter, as no counter was specified.' );
4650 }
4751
48- [array ]$CounterArray = $Counter.Split (' \' );
52+ [array ]$CounterArray = $Counter.Split (' \' );
4953 [string ]$UseCounterCategory = ' ' ;
5054 [string ]$UseCounterName = ' ' ;
5155 [string ]$UseCounterInstance = ' ' ;
@@ -72,6 +76,13 @@ function New-IcingaPerformanceCounter()
7276 # At last get the actual counter containing our values
7377 $UseCounterName = $CounterArray [2 ];
7478
79+ if ($NoCache -eq $FALSE ) {
80+ # If we are not skipping the cache, we will update the cache
81+ # with the current counter path. This will ensure that we
82+ # have a valid cache for the counter and can return it later
83+ Update-IcingaPerformanceCounterCache - Counter $Counter ;
84+ }
85+
7586 # Now as we know how the counter path is constructed and has been split into
7687 # the different values, we need to know how to handle the instances of the counter
7788
@@ -80,11 +91,13 @@ function New-IcingaPerformanceCounter()
8091 # which contains the parent name including counters for all instances that
8192 # have been found
8293 if ($UseCounterInstance -eq ' *' ) {
83- # In case we already loaded the counters once, return the finished array
84- $CachedCounter = Get-IcingaPerformanceCounterCacheItem - Counter $Counter ;
94+ if ($NoCache -eq $FALSE ) {
95+ # In case we already loaded the counters once, return the finished array
96+ $CachedCounter = Get-IcingaPerformanceCounterCacheItem - Counter $Counter ;
8597
86- if ($null -ne $CachedCounter ) {
87- return (New-IcingaPerformanceCounterResult - FullName $Counter - PerformanceCounters $CachedCounter );
98+ if ($null -ne $CachedCounter ) {
99+ return (New-IcingaPerformanceCounterResult - FullName $Counter - PerformanceCounters $CachedCounter );
100+ }
88101 }
89102
90103 # If we need to build the array, load all instances from the counters and
@@ -116,24 +129,32 @@ function New-IcingaPerformanceCounter()
116129 # Add the parent counter including the array of Performance Counters to our
117130 # caching mechanism and return the New-IcingaPerformanceCounterResult object for usage
118131 # within the monitoring modules
119- Add-IcingaPerformanceCounterCache - Counter $Counter - Instances $AllCountersInstances ;
132+ if ($NoCache -eq $FALSE ) {
133+ Add-IcingaPerformanceCounterCache - Counter $Counter - Instances $AllCountersInstances ;
134+ }
120135 return (New-IcingaPerformanceCounterResult - FullName $Counter - PerformanceCounters $AllCountersInstances );
121136 } else {
122137 # This part will handle the counters without any instances as well as
123138 # specifically assigned instances, like (_Total) CPU usage.
124139
125140 # In case we already have the counter within our cache, return the
126141 # cached informations
127- $CachedCounter = Get-IcingaPerformanceCounterCacheItem - Counter $Counter ;
142+ if ($NoCache -eq $FALSE ) {
143+ $CachedCounter = Get-IcingaPerformanceCounterCacheItem - Counter $Counter ;
128144
129- if ($null -ne $CachedCounter ) {
130- return $CachedCounter ;
145+ if ($null -ne $CachedCounter ) {
146+ return $CachedCounter ;
147+ }
131148 }
132149
133150 # If the cache is not present yet, create the Performance Counter object,
134151 # and add it to our cache
135152 $NewCounter = New-IcingaPerformanceCounterObject - FullName $Counter - Category $UseCounterCategory - Counter $UseCounterName - Instance $UseCounterInstance - SkipWait $SkipWait ;
136- Add-IcingaPerformanceCounterCache - Counter $Counter - Instances $NewCounter ;
153+ if ($NoCache -eq $FALSE ) {
154+ Add-IcingaPerformanceCounterCache - Counter $Counter - Instances $NewCounter ;
155+ } else {
156+ return $NewCounter ;
157+ }
137158 }
138159
139160 # This function will always return non-instance counters or
0 commit comments