@@ -80,17 +80,18 @@ def run():
8080 check = Check ()
8181 check .set_threshold (warning = args .warning , critical = args .critical )
8282
83- args ._si = service_instance .connect (args )
84-
8583 try :
8684 vimtype = getattr (vim , args .vimtype )
8785 except :
8886 raise Exception (f"vim.{ args .vimtype } is not known" )
8987
90- try :
91- args .perfcounter .split (":" , 2 )
92- except :
93- raise Exception ("perfcounter must be composed as groupName:perfName:rollupType" )
88+ parts = args .perfcounter .split (":" )
89+ if len (parts ) != 3 or any (not part for part in parts ):
90+ raise CheckVsphereException (
91+ "perfcounter must be composed as groupName:perfName:rollupType"
92+ )
93+
94+ args ._si = service_instance .connect (args )
9495
9596 (counter , metricId ) = get_metric (
9697 args ._si .content .perfManager , args .perfcounter , args .perfinstance
@@ -152,15 +153,24 @@ def run():
152153 code = check .check_threshold (val ),
153154 message = f'Counter { args .perfcounter } on { args .vimtype } :{ args .vimname or props ["name" ]} reported { val :.8g} { counterInfo ["unit" ]} ' ,
154155 )
156+ check .exit (
157+ code = Status .UNKNOWN ,
158+ message = (
159+ f"Counter { args .perfcounter } on { args .vimtype } :{ args .vimname or props ['name' ]} "
160+ "did not return an aggregate instance; maybe --perfinstance='*' helps"
161+ ),
162+ )
155163 else :
156164 metrics_count = len (values .value )
165+ found_instance = False
157166 for instance in values .value :
158167 if metrics_count == 1 and instance .id .instance == '' :
159168 raise CheckVsphereException ("The perfcounter only returns an aggregate, maybe you want to use --perfinstance '' instead" )
160169 if instance .id .instance == '' :
161170 # ignore the aggregate if we query a specific or all instances
162171 continue
163172 if args .perfinstance == '*' or args .perfinstance == instance .id .instance :
173+ found_instance = True
164174 val = instance .value [0 ] * counterInfo ['factor' ]
165175 check .add_perfdata (
166176 label = f'{ instance .id .instance } { args .perfcounter } ' ,
@@ -173,6 +183,16 @@ def run():
173183 f"'{ instance .id .instance } { args .perfcounter } ' has value { val :.8g} { counterInfo ['unit' ]} " ,
174184 )
175185
186+ if not found_instance :
187+ check .exit (
188+ code = Status .UNKNOWN ,
189+ message = (
190+ f"Counter { args .perfcounter } on { args .vimtype } :{ args .vimname or props ['name' ]} "
191+ f"did not return the requested instance '{ args .perfinstance } '; "
192+ "maybe --perfinstance='*' helps to examine the available instances"
193+ ),
194+ )
195+
176196 (code , message ) = check .check_messages (separator = '\n ' )
177197 check .exit (code = code , message = message )
178198
@@ -230,7 +250,7 @@ def get_argparser():
230250 'options' : {
231251 'action' : 'store' ,
232252 'default' : '' ,
233- 'help' : 'the instance of of the metric to monitor. defaults to empty string, '
253+ 'help' : 'the instance of the metric to monitor. defaults to empty string, '
234254 'which is not always available but means an aggregated value over all instances' ,
235255 },
236256 }
0 commit comments