Skip to content

Instantly share code, notes, and snippets.

@ysasaki
Last active August 24, 2023 11:30
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ysasaki/f6d87beb91277546a77c to your computer and use it in GitHub Desktop.
Save ysasaki/f6d87beb91277546a77c to your computer and use it in GitHub Desktop.
php-fpm status to AWS CloudWatch
#!/bin/bash
AWS_DEFAULT_REGION="ap-northeast-1"
AWS_ACCESS_KEY_ID="YOUR ACCESS KEY HERE"
AWS_SECRET_ACCESS_KEY="YOUR SECRET ACCESS KEY HERE"
INSTANCE_ID_URL="http://169.254.169.254/latest/meta-data/instance-id"
INSTANCE_ID=$(curl -s ${INSTANCE_ID_URL})
SERVER_STATUS_URL="http://localhost/php-fpm-status"
SERVER_STATUS_HOSTNAME="server-status.localhost"
NAMESPACE="php-fpm"
TIMESTAMP=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
PUT_METRIC_CMD="/bin/env \
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} \
AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \
AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \
/usr/bin/aws cloudwatch put-metric-data \
--region ${AWS_DEFAULT_REGION} \
--dimensions InstanceId=${INSTANCE_ID} \
--namespace ${NAMESPACE} \
--timestamp ${TIMESTAMP}"
TMP_FILE="/tmp/php-fpm-status"
# GET /php-fpm-status
curl -H"Host: ${SERVER_STATUS_HOSTNAME}" \
-s -L ${SERVER_STATUS_URL} > ${TMP_FILE}
# put-metric-data
${PUT_METRIC_CMD} --metric-name IdleProcesses --unit Count \
--value $(grep -e '^idle processes' ${TMP_FILE} | awk -F: '{printf "%i", $2}')
${PUT_METRIC_CMD} --metric-name ActiveProcesses --unit Count \
--value $(grep -e '^active processes' ${TMP_FILE} | awk -F: '{printf "%i", $2}')
${PUT_METRIC_CMD} --metric-name TotalProcesses --unit Count \
--value $(grep -e '^total processes' ${TMP_FILE} | awk -F: '{printf "%i", $2}')
${PUT_METRIC_CMD} --metric-name MaxActiveProcesses --unit Count \
--value $(grep -e '^max active processes' ${TMP_FILE} | awk -F: '{printf "%i", $2}')
${PUT_METRIC_CMD} --metric-name MaxChildrenReached --unit Count \
--value $(grep -e '^max children reached' ${TMP_FILE} | awk -F: '{printf "%i", $2}')
${PUT_METRIC_CMD} --metric-name SlowRequests --unit Count \
--value $(grep -e '^slow requests' ${TMP_FILE} | awk -F: '{printf "%i", $2}')
server {
listen 80;
server_name server-status.localhost;
root /usr/share/nginx/html;
location / {
deny all;
}
location = /php-fpm-status {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
include fastcgi.conf;
access_log off;
allow 127.0.0.1;
include ip-realworld;
deny all;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment