Giter Site home page Giter Site logo

linux-getpidstat's Introduction

NAME

Linux::GetPidstat - Monitor each process metrics avg using each pidfile

SYNOPSIS

use Linux::GetPidstat;

my $stat = Linux::GetPidstat->new;
$stat->run(%opt);

DESCRIPTION

Run pidstat -h -u -r -s -d -w -p $pid $interval $count commands in parallel to monitor each process metrics avg/1min.

Output to a specified file [and|or] mackerel service https://mackerel.io.

Motivation

A batch server runs many batch scripts at the same time.

When this server suffers by a resource short, it's difficult to grasp which processes are heavy quickly.

Running pidstat manually is not appropriate in this situation, because

  • the target processes are changed by starting each job.
  • the target processes may run child processes recursively.

Requirements

pidstat pstree

Usage

Prepare pid files in a specified directory.

$ mkdir /tmp/pid_dir
$ echo 1234 > /tmp/pid_dir/target_script
$ echo 1235 > /tmp/pid_dir/target_script2
# In production, this file is made and removed by the batch script itself for instance.

Run the script every 1 mininute.

# vi /etc/cron.d/linux-get-pidstat
* * * * * user carton exec -- linux-get-pidstat --no-dry_run --pid_dir=/tmp/pid_dir --res_dir=/tmp/bstat.log

Done, you can monitor the result.

$ tail -f /tmp/bstat.log
# start(datetime),start(epoch),pidfilename,name,value
2016-04-02T19:49:32,1459594172,target_script,cswch_per_sec,19.87
2016-04-02T19:49:32,1459594172,target_script,stk_ref,25500
2016-04-02T19:49:32,1459594172,target_script,memory_percent,34.63
2016-04-02T19:49:32,1459594172,target_script,memory_rss,10881534000
2016-04-02T19:49:32,1459594172,target_script,stk_size,128500
2016-04-02T19:49:32,1459594172,target_script,nvcswch_per_sec,30.45
2016-04-02T19:49:32,1459594172,target_script,cpu,21.2
2016-04-02T19:49:32,1459594172,target_script,disk_write_per_sec,0
2016-04-02T19:49:32,1459594172,target_script,disk_read_per_sec,0
2016-04-02T19:49:32,1459594172,target_script2,memory_rss,65289204000
2016-04-02T19:49:32,1459594172,target_script2,memory_percent,207.78
2016-04-02T19:49:32,1459594172,target_script2,stk_ref,153000
2016-04-02T19:49:32,1459594172,target_script2,cswch_per_sec,119.22
2016-04-02T19:49:32,1459594172,target_script2,nvcswch_per_sec,182.7
2016-04-02T19:49:32,1459594172,target_script2,cpu,127.2
2016-04-02T19:49:32,1459594172,target_script2,disk_read_per_sec,0
2016-04-02T19:49:32,1459594172,target_script2,disk_write_per_sec,0
2016-04-02T19:49:32,1459594172,target_script2,stk_size,771000

Mackerel

Post the results to service metrics.

$ carton exec -- linux-get-pidstat \
--no-dry_run \
--pid_dir=/tmp/pid_dir \
--mackerel_api_key=yourkey \
--mackerel_service_name=yourservice

Help

Display how to use.

$ carton exec -- linux-get-pidstat --help
Usage:
        linux-get-pidstat - command description
          Usage: command [options]
          Options:
            --pid_dir                     A directory path for pid files
            --res_file                    A file path to be stored results
            --interval                    Interval second to be given as a pidstat argument (default:1)
            --count                       Count number to be given as a pidstat argument (default:60)
            --dry_run                     Dry run mode. not run the side-effects operation (default:1) (--no-dry_run is also supported)
            --datetime                    Datetime (ex. '2016-06-10 00:00:00') to be recorded
            --include_child               Flag to be enabled to include child process metrics (default:1) (--no-include_child is also suppoted)
            --max_child_limit             Number to be used for limiting pidstat multi processes (default:30) (skip this limit if 0 is specified)
            --mackerel_metric_type        Metric type of mackerel (default:service) (only use one of 'service' or 'host')
            --mackerel_api_key            An api key to be used for posting to mackerel
            --mackerel_service_name       An mackerel service name
            --mackerel_metric_key_prefix  Key prefix of mackerel metric name (default:batch_)
            --mackerel_host_id            An mackerel host id
          Requirement Programs: pidstat and pstree commands

LICENSE

Copyright (C) yoheimuta.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

yoheimuta [email protected]

linux-getpidstat's People

Contributors

songmu avatar waniji avatar yoheimuta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

waniji songmu

linux-getpidstat's Issues

uuv $pid in Reader.pm

Use of uninitialized value $pid in scalar chomp at /path/to/local/lib/perl5/Linux/GetPidstat/Reader.pm line 22.
Use of uninitialized value $pid in pattern match (m//) at /path/to/local/lib/perl5/Linux/GetPidstat/Reader.pm line 88.
Use of uninitialized value $pid in concatenation (.) or string at /path/to/local/lib/perl5/Linux/GetPidstat/Reader.pm line 89.
invalid pid:  at /path/to/local/lib/perl5/Linux/GetPidstat.pm line 48.
Error open (<) on '/var/tmp/make_ad_mute_index': No such file or directory at /path/to/local/lib/perl5/Linux/GetPidstat/Reader.pm line 22.

Test suite started to fail

The test suite started to fail like this:

t/00_compile.t ........... ok
t/01_new.t ............... ok
invalid pid: one at /usr/home/cpansand/.cpan/build/2022021722/Linux-GetPidstat-0.10-0/blib/lib/Linux/GetPidstat.pm line 37.
invalid pid: dummy at /usr/home/cpansand/.cpan/build/2022021722/Linux-GetPidstat-0.10-0/blib/lib/Linux/GetPidstat.pm line 37.
Empty metric: name=cpu, lines=Linux 3.13.0-83-generic (ip-10-0-1-51)  04/02/2016      _x86_64_        (4 CPU),,#      Time   UID       PID    %usr %system  %guest    %CPU   CPU  minflt/s  majflt/s     VSZ    RSS   %MEM StkSize  StkRef   kB_rd/s   kB_wr/s kB_ccwr/s   cswch/s nvcswch/s  Command
Failed getting pidstat: pid=31908, target_pids=2,18352,18353,18360,18366,28264, program_name=target_script2
Empty metric: name=cpu, lines=Linux 3.13.0-83-generic (ip-10-0-1-51)  04/02/2016      _x86_64_        (4 CPU),,#      Time   UID       PID    %usr %system  %guest    %CPU   CPU  minflt/s  majflt/s     VSZ    RSS   %MEM StkSize  StkRef   kB_rd/s   kB_wr/s kB_ccwr/s   cswch/s nvcswch/s  Command
Failed getting pidstat: pid=31909, target_pids=1, program_name=target_script

    #   Failed test at t/02_basic.t line 146.
    #     Structures begin differing at:
    #          $got->[1] = '2016-06-10T00:00:00,1465484400,target_script,cswch_per_sec,19.9'
    #     $expected->[1] = '2016-06-10T00:00:00,1465484400,target_script,cswch_per_sec,19.87'
    # 2016-06-10T00:00:00,1465484400,target_script2,nvcswch_per_sec,182.4
    # 2016-06-10T00:00:00,1465484400,target_script2,stk_ref,153000
    # 2016-06-10T00:00:00,1465484400,target_script2,cswch_per_sec,119.4
    # 2016-06-10T00:00:00,1465484400,target_script2,stk_size,768000
    # 2016-06-10T00:00:00,1465484400,target_script2,memory_percent,207.6
    # 2016-06-10T00:00:00,1465484400,target_script2,disk_write_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script2,disk_read_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script2,memory_rss,65400000000
    # 2016-06-10T00:00:00,1465484400,target_script2,cpu,127.2
    # 2016-06-10T00:00:00,1465484400,target_script,cswch_per_sec,19.9
    # 2016-06-10T00:00:00,1465484400,target_script,stk_size,128000
    # 2016-06-10T00:00:00,1465484400,target_script,nvcswch_per_sec,30.4
    # 2016-06-10T00:00:00,1465484400,target_script,stk_ref,25500
    # 2016-06-10T00:00:00,1465484400,target_script,cpu,21.2
    # 2016-06-10T00:00:00,1465484400,target_script,memory_rss,10900000000
    # 2016-06-10T00:00:00,1465484400,target_script,memory_percent,34.6
    # 2016-06-10T00:00:00,1465484400,target_script,disk_write_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script,disk_read_per_sec,0
    # Looks like you failed 1 test of 2.

#   Failed test 'output to a file'
#   at t/02_basic.t line 153.

    #   Failed test at t/02_basic.t line 189.
    #     Structures begin differing at:
    #          $got->[1] = '2016-06-10T00:00:00,1465484400,target_script,cswch_per_sec,19.9'
    #     $expected->[1] = '2016-06-10T00:00:00,1465484400,target_script,cswch_per_sec,19.87'
    # 2016-06-10T00:00:00,1465484400,target_script2,cpu,127.2
    # 2016-06-10T00:00:00,1465484400,target_script2,memory_rss,65400000000
    # 2016-06-10T00:00:00,1465484400,target_script2,disk_read_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script2,memory_percent,207.6
    # 2016-06-10T00:00:00,1465484400,target_script2,disk_write_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script2,cswch_per_sec,119.4
    # 2016-06-10T00:00:00,1465484400,target_script2,stk_size,768000
    # 2016-06-10T00:00:00,1465484400,target_script2,stk_ref,153000
    # 2016-06-10T00:00:00,1465484400,target_script2,nvcswch_per_sec,182.4
    # 2016-06-10T00:00:00,1465484400,target_script,nvcswch_per_sec,30.4
    # 2016-06-10T00:00:00,1465484400,target_script,stk_ref,25500
    # 2016-06-10T00:00:00,1465484400,target_script,stk_size,128000
    # 2016-06-10T00:00:00,1465484400,target_script,cswch_per_sec,19.9
    # 2016-06-10T00:00:00,1465484400,target_script,memory_rss,10900000000
    # 2016-06-10T00:00:00,1465484400,target_script,memory_percent,34.6
    # 2016-06-10T00:00:00,1465484400,target_script,disk_write_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script,disk_read_per_sec,0
    # 2016-06-10T00:00:00,1465484400,target_script,cpu,21.2

    #   Failed test at t/02_basic.t line 193.
    #     Structures begin differing at:
    #          $got->[2] = 'mackerel post: name=custom.batch_cswch_per_sec.target_script, time=1465484400, metric=19.9'
    #     $expected->[2] = 'mackerel post: name=custom.batch_cswch_per_sec.target_script, time=1465484400, metric=19.87'
    # mackerel post: name=custom.batch_cpu.target_script2, time=1465484400, metric=127.2
    # mackerel post: name=custom.batch_memory_rss.target_script2, time=1465484400, metric=65400000000
    # mackerel post: name=custom.batch_disk_read_per_sec.target_script2, time=1465484400, metric=0
    # mackerel post: name=custom.batch_memory_percent.target_script2, time=1465484400, metric=207.6
    # mackerel post: name=custom.batch_disk_write_per_sec.target_script2, time=1465484400, metric=0
    # mackerel post: name=custom.batch_cswch_per_sec.target_script2, time=1465484400, metric=119.4
    # mackerel post: name=custom.batch_stk_size.target_script2, time=1465484400, metric=768000
    # mackerel post: name=custom.batch_stk_ref.target_script2, time=1465484400, metric=153000
    # mackerel post: name=custom.batch_nvcswch_per_sec.target_script2, time=1465484400, metric=182.4
    # mackerel post: name=custom.batch_nvcswch_per_sec.target_script, time=1465484400, metric=30.4
    # mackerel post: name=custom.batch_stk_ref.target_script, time=1465484400, metric=25500
    # mackerel post: name=custom.batch_stk_size.target_script, time=1465484400, metric=128000
    # mackerel post: name=custom.batch_cswch_per_sec.target_script, time=1465484400, metric=19.9
    # mackerel post: name=custom.batch_memory_rss.target_script, time=1465484400, metric=10900000000
    # mackerel post: name=custom.batch_memory_percent.target_script, time=1465484400, metric=34.6
    # mackerel post: name=custom.batch_disk_write_per_sec.target_script, time=1465484400, metric=0
    # mackerel post: name=custom.batch_disk_read_per_sec.target_script, time=1465484400, metric=0
    # mackerel post: name=custom.batch_cpu.target_script, time=1465484400, metric=21.2
    # Looks like you failed 2 tests of 2.

#   Failed test 'output to a file and mackerel'
#   at t/02_basic.t line 198.
# Looks like you failed 2 tests of 7.
t/02_basic.t ............. 
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/7 subtests 
... etc, more similar failures following

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.