Skip to content

Instantly share code, notes, and snippets.

@buty4649
Last active December 24, 2025 04:23
Show Gist options
  • Select an option

  • Save buty4649/53dae47e68c691738d26499948712c7a to your computer and use it in GitHub Desktop.

Select an option

Save buty4649/53dae47e68c691738d26499948712c7a to your computer and use it in GitHub Desktop.
プロセスの数、CPU使用率、メモリ使用率(RSS/VSZ)を取るプラグインです
#!/bin/bash
# MIT License
#
# Copyright (c) 2025 buty4649
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Mackerel plugin for process monitoring
# Usage: ./mackerel-plugin-proc-stats process_name
process_name="$1"
if [ -z "$process_name" ]; then
echo "Usage: $0 process_name" >&2
exit 1
fi
# Output meta information when MACKEREL_AGENT_PLUGIN_META is set
if [ "$MACKEREL_AGENT_PLUGIN_META" = "1" ]; then
cat << EOF
# mackerel-agent-plugin
{
"graphs": {
"proc_stats.${process_name}": {
"label": "Process ${process_name}",
"unit": "integer",
"metrics": [
{
"name": "count",
"label": "Process Count"
},
{
"name": "cpu_percent",
"label": "CPU Usage (%)"
},
{
"name": "rss",
"label": "RSS Memory",
"unit": "bytes"
},
{
"name": "vsz",
"label": "VSZ Memory",
"unit": "bytes"
}
]
}
}
}
EOF
exit 0
fi
timestamp=$(date +%s)
prefix="proc_stats.${process_name}"
# Get process metrics using ps and awk
ps -C "$process_name" -o %cpu,rss,vsz --no-headers 2>/dev/null | awk -v prefix="$prefix" -v timestamp="$timestamp" '
BEGIN {
total_cpu = 0
total_rss = 0
total_vsz = 0
process_count = 0
}
{
if (NF >= 3 && $1 ~ /^[0-9]*\.?[0-9]+$/ && $2 ~ /^[0-9]+$/ && $3 ~ /^[0-9]+$/) {
total_cpu += $1
total_rss += $2 * 1024 # KB to Bytes
total_vsz += $3 * 1024 # KB to Bytes
process_count++
}
}
END {
if (process_count == 0) {
exit 1
}
print prefix ".count\t" process_count "\t" timestamp
print prefix ".cpu_percent\t" total_cpu "\t" timestamp
print prefix ".rss\t" total_rss "\t" timestamp
print prefix ".vsz\t" total_vsz "\t" timestamp
}'
# Check awk exit status and handle process not found
if [ $? -ne 0 ]; then
echo "Process '$process_name' not found" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment