1515"""
1616
1717import os
18+ from shlex import split
1819import sys
1920import subprocess
2021from re import findall , match
2526def get_args ():
2627 """ Parse Arguments """
2728 parser = ArgumentParser (
28- description = "Icinga/Nagios plugin which checks health and statistics of a \
29+ description = "Icinga/Nagios plugin which checks health and statistics of a \
2930 Container" )
3031 parser .add_argument ("-c" , "--container" , required = True ,
3132 help = "Name of the Container which should be checked" ,
3233 type = str , dest = 'container_name' )
34+ parser .add_argument ("-u" , "--user" , required = False ,
35+ help = "User for rootless containers, default: root" ,
36+ default = 'root' ,
37+ type = str , dest = 'user' )
3338 parser .add_argument ("-t" , "--timeout" , required = False ,
3439 help = "timeout in seconds" , type = int , dest = 'timeout' ,
3540 default = 10 )
@@ -72,22 +77,24 @@ def exit_plugin(returncode, output, perfdata):
7277 sys .exit (0 )
7378
7479
75- def get_container_pslist (args , docker_env ):
76- """ execute docker ps"""
80+ def get_container_pslist (args , podman_env ):
81+ """ execute podman ps"""
7782
78- # Execute "docker ps" command
79- result = subprocess .run (['podman' , 'ps' , '-a' , '-f' , f'name=^{ args .container_name } $' ,
80- '--format' , '"{{.Names}},{{.Status}},{{.Size}},{{.RunningFor}}"' ,
81- '--size' ],
82- shell = False ,
83- check = False ,
84- env = docker_env ,
85- stdout = subprocess .PIPE ,
86- stderr = subprocess .PIPE )
83+ # Execute "podman ps" command
84+ result = subprocess .run (
85+ split ((f'su - { args .user } /bin/bash -c "podman ps -a'
86+ f' -f name=^{ args .container_name } $ --format'
87+ ' {{.Names}},{{.Status}},{{.Size}},{{.RunningFor}}'
88+ ' --size"' )),
89+ shell = False ,
90+ check = False ,
91+ env = podman_env ,
92+ stdout = subprocess .PIPE ,
93+ stderr = subprocess .PIPE )
8794
8895 # Check if command exited without error code
8996 if result .returncode != 0 :
90- exit_plugin (3 , f'docker stats command returned error: { result .stderr } ' , '' )
97+ exit_plugin (3 , f'podman ps command returned error: { result .stderr } ' , '' )
9198
9299 # Check if the command returned any putput
93100 elif len (result .stdout .decode ()) == 0 :
@@ -111,26 +118,28 @@ def get_container_pslist(args, docker_env):
111118 return container_ps
112119
113120
114- def get_container_stats (args , docker_env ):
115- """ execute docker stat """
121+ def get_container_stats (args , podman_env ):
122+ """ execute podman stats """
116123
117- # Execute "docker stats" command
118- result = subprocess .run (['podman' , 'stats' , args .container_name , '--no-stream' , '--format' ,
119- '"{{.Name}},{{.ID}},{{.CPUPerc}},{{.MemUsage}},\
120- {{.NetIO}},{{.BlockIO}},{{.PIDs}}"' ],
121- shell = False ,
122- check = False ,
123- env = docker_env ,
124- stdout = subprocess .PIPE ,
125- stderr = subprocess .PIPE )
124+ # Execute "podman stats" command
125+ result = subprocess .run (
126+ split ((f'su - { args .user } /bin/bash -c "'
127+ f' podman stats { args .container_name } --no-stream --format'
128+ ' {{.Name}},{{.ID}},{{.CPUPerc}},{{.MemUsage}}'
129+ ',{{.NetIO}},{{.BlockIO}},{{.PIDs}}"' )),
130+ shell = False ,
131+ check = False ,
132+ env = podman_env ,
133+ stdout = subprocess .PIPE ,
134+ stderr = subprocess .PIPE )
126135
127136 # Check if command exited without error code
128137 if result .returncode != 0 :
129- exit_plugin (3 , f'docker stats command returned error: { result .stderr } ' , '' )
138+ exit_plugin (3 , f'podman stats command returned error: { result .stderr } ' , '' )
130139
131140 # Check if the command returned any putput
132141 elif len (result .stdout .decode ()) == 0 :
133- exit_plugin (2 , (f'docker stats did not return any output for '
142+ exit_plugin (2 , (f'podman stats did not return any output for '
134143 f'Container { args .container_name } ' ), '' )
135144
136145 # Initialize return object
@@ -166,7 +175,7 @@ def get_container_stats(args, docker_env):
166175
167176
168177def convert_to_bytes (inputstr ):
169- """ converts docker output units to raw bytes """
178+ """ converts podman output units to raw bytes """
170179
171180 if inputstr == "--" :
172181 # If the container does not bind any ports podman returns "-- / --" as
@@ -208,12 +217,12 @@ def main():
208217 # Get Arguments
209218 args = get_args ()
210219
211- # environment variables for "docker " command
212- docker_env = os .environ
220+ # environment variables for "podman " command
221+ podman_env = os .environ
213222
214- # Execute "docker ps" and "docker stats"
215- container_ps = get_container_pslist (args , docker_env )
216- container_stats = get_container_stats (args , docker_env )
223+ # Execute "podman ps" and "podman stats"
224+ container_ps = get_container_pslist (args , podman_env )
225+ container_stats = get_container_stats (args , podman_env )
217226
218227 # Construct perfdata and output
219228 output = (f"{ container_stats ['name' ]} ({ container_stats ['id' ]} ) is { container_ps ['state' ]} - "
@@ -238,16 +247,18 @@ def main():
238247 returncode = 1
239248 if args .cpucrit is not None and args .cpucrit < container_stats ['cpu_perc' ]:
240249 returncode = 2
241- if args .cpuwarn is not None and args .cpuwarn < container_stats ['cpu_perc' ] and returncode != 2 :
250+ if (args .cpuwarn is not None and args .cpuwarn < container_stats ['cpu_perc' ]
251+ and returncode != 2 ):
242252 returncode = 1
243253 if args .memcrit is not None and args .memcrit < container_stats ['mem_used_byte' ]:
244254 returncode = 2
245- if args .memwarn is not None and args .memwarn < container_stats ['mem_used_byte' ] \
246- and returncode != 2 :
255+ if ( args .memwarn is not None and args .memwarn < container_stats ['mem_used_byte' ]
256+ and returncode != 2 ) :
247257 returncode = 1
248258 if args .pidcrit is not None and args .pidcrit < container_stats ['pids' ]:
249259 returncode = 2
250- if args .pidwarn is not None and args .pidwarn < container_stats ['pids' ] and returncode != 2 :
260+ if (args .pidwarn is not None and args .pidwarn < container_stats ['pids' ]
261+ and returncode != 2 ):
251262 returncode = 1
252263
253264 exit_plugin (returncode , output , perfdata )
0 commit comments