Plugins PHP-FPM

I couldn’t sleep before I solved this:

The /etc/check_mk/php_fpm_pools.cfg now can be configured with a tuple containing host and port. Also, the check can work with both IPv4 and IPv6 as well as DNS resolution.

/etc/check_mk/php_fpm_pools.cfg

php_fpm = [
  {"socket": "/var/run/php/php-fpm.sock", "path": "/status"},
  {"socket": ("localhost", 9000)},
]

Here’s the new diff:

36c36
< #  {"socket": ("localhost", 9000)},
---
> #  {"socket": "localhost:9000"},
77,78d76
<         if type(self.socket_path) is tuple:
<            self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
81,83c79,82
<         except Exception as e:
<             sys.stderr.write("Socket: " + str(self.socket_path) + ", Path: " + self.status_path + "\n")
<             sys.stderr.write(str(e) + "\n")
---
>         except:
>             print(self.socket_path, self.status_path)
>             print(sys.exc_info()[1])
>             #sys.exit(1)
117d115
<                 self.status_data = self.raw_status_data.decode().split("\r\n\r\n")[1]
124,126c122,125
<
<         except Exception as e:
<             sys.stderr.write(str(e) + "\n")
---
>         except:
>             print(sys.exc_info()[1])
>             sys.exit(2)
>         self.status_data = self.raw_status_data.decode().split("\r\n\r\n")[1]
136,142c135,140
<         if hasattr(self, 'status_data'):
<             data = json.loads(self.status_data)
<             pool_name = data.pop('pool', None)
<             pm_type = data.pop('process manager', None)
<             for key in data:
<                 spaceless_key = "_".join( key.split() )
<                 sys.stdout.write("%s %s %s %s\n" % (pool_name, pm_type, spaceless_key, str(data[key])))
---
>         data = json.loads(self.status_data)
>         pool_name = data.pop('pool', None)
>         pm_type = data.pop('process manager', None)
>         for key in data:
>             spaceless_key = "_".join( key.split() )
>             sys.stdout.write("%s %s %s %s\n" % (pool_name, pm_type, spaceless_key, str(data[key])))
167d164
<         continue

I will write the author tomorrow and ask if he can merge my changes and add me as contributor.

Good night!

PS: The script will be printing error messages to stderr, so a human can read them manually running the script while the agent will not get confused with unintentional output:

root@host:~# ./php_fpm_pools
<<<php_fpm_pools>>>
Socket: /var/run/php/php-fpm.sock, Path: /status
[Errno 2] No such file or directory
[Errno 107] Transport endpoint is not connected
www dynamic active_processes 1
www dynamic accepted_conn 196
www dynamic listen_queue 0
www dynamic start_since 5084
www dynamic idle_processes 11
www dynamic start_time 1588117405
www dynamic slow_requests 0
www dynamic max_active_processes 3
www dynamic max_children_reached 0
www dynamic max_listen_queue 0
www dynamic total_processes 12
www dynamic listen_queue_len 128
root@host:~# ./php_fpm_pools 2> /dev/null
<<<php_fpm_pools>>>
www dynamic active_processes 1
www dynamic accepted_conn 197
www dynamic listen_queue 0
www dynamic start_since 5087
www dynamic idle_processes 11
www dynamic start_time 1588117405
www dynamic slow_requests 0
www dynamic max_active_processes 3
www dynamic max_children_reached 0
www dynamic max_listen_queue 0
www dynamic total_processes 12
www dynamic listen_queue_len 128
1 Like