Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kouni/b692867176711b27efa9720b84640dd8 to your computer and use it in GitHub Desktop.
Save Kouni/b692867176711b27efa9720b84640dd8 to your computer and use it in GitHub Desktop.

Revisions

  1. @ysasaki ysasaki renamed this gist Jul 22, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion nginx-server-status.conf → nginx_conf.d_server-status.conf
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,5 @@ server {
    allow 127.0.0.1;
    include ip-realworld;
    deny all;
    }
    }
    }
  2. @ysasaki ysasaki created this gist Jul 22, 2015.
    49 changes: 49 additions & 0 deletions cloudwatch-php-fpm-status.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #!/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}')
    17 changes: 17 additions & 0 deletions nginx-server-status.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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;
    }