Other Sử dụng Munin để hiển thị số liệu thống kê bộ nhớ cache APC

PVS

Super Moderator
Thành viên BQT
Tham gia
28/02/2015
Bài viết
16,829
Được Like
12,687
Sử dụng Munin để hiển thị số liệu thống kê bộ nhớ cache APC

Tôi sử dụng nginx cho máy chủ http của tôi, và cấu hình này được cấu hình trên Debian 6.0. Hãy để sửa đổi nó cho apache hoặc bất kỳ hệ điều hành Linux khác nếu đó là những gì bạn sử dụng. Không hỗ trợ được cung cấp - đây là từ thông tin mà tôi thu thập được từ đó công cụ tuyệt vời được gọi là Google và muốn biên dịch nó vào một nơi để người khác sử dụng.
Nếu bạn muốn tham khảo các trang web mà tôi thu được các thông tin từ, nó nằm ở đây. Có hướng dẫn về cách sử dụng wget để có được apc_info.phpphp_apc_ nếu bạn không muốn cắt và vừa qua. Sử dụng wget cũng sẽ nhận được phiên bản mới nhất của code.

Trong /etc/munin/plugin-conf.d tạo ra một tập tin gọi là php_apc chứa
Mã:
[php_apc_*]
user root
env.url http://localhost/apc_info.php?auto

Xác định một localhost cho nginx với
Mã:
server {
    server_name localhost;
    include /etc/nginx/php.conf;
    root /usr/share/nginx/html;
    allow 127.0.0.1;
    deny all;
    location / {
    access_log off;
        }
    location /nginx_status {
    stub_status on;
    access_log off;
        }
            }

Trong ROOT định nghĩa ở trên tạo ra một tập tin gọi là apc_info.php chứa
Mã:
<?php
/**
* TODO: File header.
* TODO: Code comments.
*/

if(function_exists("apc_cache_info") && function_exists("apc_sma_info")) {

  $time = time();

  $mem = apc_sma_info();
  $mem_size = $mem['num_seg']*$mem['seg_size'];
  $mem_avail= $mem['avail_mem'];
  $mem_used = $mem_size-$mem_avail;

  // Some code taken from the file apc.php by The PHP Group.
  $nseg = $freeseg = $fragsize = $freetotal = 0;
  for($i=0; $i<$mem['num_seg']; $i++) {
    $ptr = 0;
    foreach($mem['block_lists'][$i] as $block) {
      if ($block['offset'] != $ptr) {
        ++$nseg;
      }
      $ptr = $block['offset'] + $block['size'];
      // Only consider blocks <5M for the fragmentation %
      if($block['size']<(5*1024*1024)) $fragsize+=$block['size'];
      $freetotal+=$block['size'];
    }
    $freeseg += count($mem['block_lists'][$i]);
  }

  if ($freeseg < 2) {
    $fragsize = 0;
    $freeseg = 0;
  }

  $cache_mode = 'opmode';
  $cache=@apc_cache_info($cache_mode);

  // Item hits, misses and inserts
  $hits = $cache['num_hits'];
  $misses = $cache['num_misses'];
  $inserts = $cache['num_inserts'];

  //
  $req_rate = ($cache['num_hits']+$cache['num_misses'])/($time-$cache['start_time']);
  $hit_rate = ($cache['num_hits'])/($time-$cache['start_time']); // Number of entries in cache $number_entries = $cache['num_entries'];
  $miss_rate = ($cache['num_misses'])/($time-$cache['start_time']); // Total number of cache purges $purges = $cache['expunges'];
  $insert_rate = ($cache['num_inserts'])/($time-$cache['start_time']);

  // Number of entries in cache
  $number_entries = $cache['num_entries'];

  // Total number of cache purges
  $purges = $cache['expunges'];

  //apc_clear_cache($cache_mode);

  $out = array(
    'size: ' . sprintf("%.2f", $mem_size),
    'used: ' . sprintf("%.2f", $mem_used),
    'free: ' . sprintf("%.2f", $mem_avail - $fragsize),
    'hits: ' . sprintf("%.2f", $hits * 100 / ($hits + $misses)),
    'misses: ' . sprintf("%.2f", $misses * 100 / ($hits + $misses)),
    'request_rate: ' . sprintf("%.2f", $req_rate),
    'hit_rate: ' . sprintf("%.2f", $hit_rate),
    'miss_rate: ' . sprintf("%.2f", $miss_rate),
    'insert_rate: ' . sprintf("%.2f", $insert_rate),
    'entries: ' . $number_entries,
    'inserts: ' . $inserts,
    'purges: ' . $purges,

  // TODO: Delete
    'purge_rate: ' . sprintf("%.2f", (100 - ($number_entries / $inserts) * 100)),
  // TODO: Delete
    'fragment_percentage: ' . sprintf("%.2f", ($fragsize/$mem_avail)*100),
    'fragmented: ' . sprintf("%.2f", $fragsize),
    'fragment_segments: ' . $freeseg,
  );
}
else {
  $out = array('APC-not-installed');
}
echo implode(' ', $out);

Tạo một tập tin trong thư mục /etc/nginx của bạn được gọi là php.conf chứa
Mã:
index index.php index.html index.htm;

location ~ \.php$ {
  # Zero-day exploit defense.
  # http://forum.nginx.org/read.php?2,88845,page=3
  # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
  # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
  try_files $uri =404;

  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  include /etc/nginx/fastcgi_params;

  # As explained in http://kbeezie.com/view/php-self-path-nginx/ some fastcgi_param are missing from fastcgi_params.
  # Keep these parameters for compatibility with old PHP scripts using them.
  fastcgi_param PATH_INFO      $fastcgi_path_info;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  # Some default config
  fastcgi_connect_timeout        60;
  fastcgi_send_timeout          180;
  fastcgi_read_timeout          180;
  fastcgi_busy_buffers_size    256k;
  fastcgi_temp_file_write_size 256k;

  fastcgi_intercept_errors    on;
  fastcgi_ignore_client_abort off;

  fastcgi_pass 127.0.0.1:9000;
}

* Chú ý: - một số có thể được lặp lại ở nginx.conf của bạn, nếu như vậy, loại bỏ chúng khỏi đây sau khi tạo ra các tập tin và phát lệnh nginx -t để kiểm tra cấu hình. Nếu có trùng lặp, nó sẽ cảnh báo bạn *

Tạo một tập tin gọi là php_apc_ trong /usr/share/munin/plugins chứa
Mã:
#!/usr/bin/perl
#
# Magic markers:
#%# family=auto
#%# capabilities=autoconf

use strict;
use Munin::Plugin;

need_multigraph();

my $ret = undef;

if (! eval "require LWP::UserAgent;")
{
    $ret = "LWP::UserAgent not found";
}

my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/apc_info.php?auto";
my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80);

if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" )
{
    if ($ret)
    {
        print "no ($ret)\n";
        exit 1;
    }

    my $ua = LWP::UserAgent->new(timeout => 30);

    my @badports;
    foreach my $port (@PORTS) {
        my $url = sprintf $URL, $port;
        my $response = $ua->request(HTTP::Request->new('GET',$url));
        push @badports, $port unless $response->is_success and $response->content =~ /^size:/im;
    }
    if (@badports) {
        print "no (apc-status)\n";
        exit 1;
    } else {
        print "yes\n";
        exit 0;
    }
}

if ( defined $ARGV[0] and $ARGV[0] eq "config" )
{

$0 =~ /php_apc_(.+)*/;
my $plugin = $1;

## PHP APC Cache Usage
if($plugin eq 'usage') {
print('multigraph php_apc_usage
graph_title Cache Usage
graph_args --base 1024 -l 0
graph_vlabel Bytes
graph_category php-apc
graph_order used free
graph_total Total
used.label Used Memory
used.draw AREASTACK
fragmented.label Fragmented Memory
fragmented.draw AREASTACK
free.label Available Memory
free.draw AREASTACK
');
}

## PHP APC Hit / Miss by percentage
elsif($plugin eq 'hit_miss') {
print('multigraph php_apc_hit_miss
graph_title Cache Hits / Misses
graph_args --base 1000 --lower-limit 0 --upper-limit 100 --rigid
graph_vlabel Percent
graph_category php-apc
graph_total Total
hits.label Hits
hits.draw AREA
hits.min 0
misses.label Misses
misses.draw STACK
misses.min 0
misses.warning 50
');
}

## PHP APC Purge rate (# Entries / Inserts)
elsif($plugin eq 'purge') {
print('multigraph php_apc_purge
graph_title Purge rate
graph_args --base 1000 --lower-limit 0 --upper-limit 100 --rigid
graph_vlabel Percent
graph_category php-apc
purge_rate.label Purge Rate
purge_rate.draw LINE2
purge_rate.min 0
purge_rate.warning 10
');
}

## PHP APC Fragmentation
elsif($plugin eq 'fragmentation') {
print('multigraph php_apc_fragmentation
graph_title Fragmentation
graph_args --base 1000 --upper-limit 100
graph_vlabel Percent
graph_category php-apc
fragment_percentage.label Fragmentation Percent
fragment_percentage.draw LINE2
fragment_percentage.min 0
fragment_percentage.warning 10
');
}

## PHP APC Number of files in cache
elsif($plugin eq 'files') {
print('multigraph php_apc_files
graph_title Files in Cache
graph_args -l 0
graph_vlabel Number of Files
graph_category php-apc
entries.label Number of files
entries.draw LINE2
entries.min 0
');
}

## PHP APC Rates
elsif($plugin eq 'rates') {
print('multigraph php_apc_rates
graph_title Hit, Miss and Insert Rates
graph_args --base 1000
graph_vlabel Cache Requests / Second
graph_category php-apc
request_rate.label Request rate (Hits + Misses)
request_rate.draw LINE2
request_rate.min 0
hit_rate.label Hit rate
hit_rate.draw LINE2
hit_rate.min 0
miss_rate.label Miss rate
miss_rate.draw LINE2
miss_rate.min 0
insert_rate.label Insert rate
insert_rate.draw LINE2
insert_rate.min 0
');
}

exit 0;
}

foreach my $port (@PORTS)
{
    my $ua = LWP::UserAgent->new(timeout => 30);
    my $url = sprintf $URL, $port;
    my $response = $ua->request(HTTP::Request->new('GET',$url));

    if ($response->content =~ /used:\s+([0-9\.]+)/im) {
        print "used.value $1\n";
    } else {
        print "used.value U\n";
    }
    if ($response->content =~ /free:\s+([0-9\.]+)/im) {
            print "free.value $1\n";
    } else {
            print "free.value U\n";
    }

    if ($response->content =~ /hits:\s+([0-9\.]+)/im) {
            print "hits.value $1\n";
    } else {
            print "hits.value U\n";
    }

    if ($response->content =~ /misses:\s+([0-9\.]+)/im) {
            print "misses.value $1\n";
    } else {
            print "misses.value U\n";
    }

    if ($response->content =~ /request_rate:\s+([0-9\.]+)/im) {
            print "request_rate.value $1\n";
    } else {
            print "request_rate.value U\n";
    }

    if ($response->content =~ /hit_rate:\s+([0-9\.]+)/im) {
            print "hit_rate.value $1\n";
    } else {
            print "hit_rate.value U\n";
    }

    if ($response->content =~ /miss_rate:\s+([0-9\.]+)/im) {
            print "miss_rate.value $1\n";
    } else {
            print "miss_rate.value U\n";
    }

    if ($response->content =~ /insert_rate:\s+([0-9\.]+)/im) {
            print "insert_rate.value $1\n";
    } else {
            print "insert_rate.value U\n";
    }

    if ($response->content =~ /entries:\s+([0-9]+)/im) {
            print "entries.value $1\n";
    } else {
            print "entries.value U\n";
    }

    if ($response->content =~ /inserts:\s+([0-9]+)/im) {
            print "inserts.value $1\n";
    } else {
            print "inserts.value U\n";
    }

    if ($response->content =~ /purges:\s+([0-9]+)/im) {
            print "purges.value $1\n";
    } else {
            print "purges.value U\n";
    }

    if ($response->content =~ /purge_rate:\s+([0-9\.]+)/im) {
            print "purge_rate.value $1\n";
    } else {
            print "purge_rate.value U\n";
    }

    if ($response->content =~ /fragmented:\s+([0-9\.]+)/im) {
            print "fragmented.value $1\n";
    } else {
            print "fragmented.value U\n";
    }

    if ($response->content =~ /fragment_segments:\s+([0-9]+)/im) {
            print "fragment_segments.value $1\n";
    } else {
            print "fragment_segments.value U\n";
    }

    if ($response->content =~ /fragment_percentage:\s+([0-9\.]+)/im) {
            print "fragment_percentage.value $1\n";
    } else {
            print "fragment_percentage.value U\n";
    }
}

# vim:syntax=perl

xét dòng lệnh vấn đề
Mã:
chmod -R 755 /usr/share/munin/plugins/php_apc_
ln -s /usr/share/munin/plugins/php_apc_ /etc/munin/plugins/php_apc_usage
ln -s /usr/share/munin/plugins/php_apc_ /etc/munin/plugins/php_apc_hit_miss
ln -s /usr/share/munin/plugins/php_apc_ /etc/munin/plugins/php_apc_purge
ln -s /usr/share/munin/plugins/php_apc_ /etc/munin/plugins/php_apc_fragmentation
ln -s /usr/share/munin/plugins/php_apc_ /etc/munin/plugins/php_apc_files
ln -s /usr/share/munin/plugins/php_apc_ /etc/munin/plugins/php_apc_rates

Bạn phải có cài đặt libwww-perl. Khi ở trên là hoàn tất, khởi động lại phát hành một /etc/init.d/munin-node (tùy thuộc vào hệ điều hành của bạn, bạn có thể phải sử dụng khởi động lại su /etc/init.d/munin-node) từ dấu nhắc. Bạn nên bắt đầu thấy đồ họa sớm. Nếu không, hãy chắc chắn để kiểm tra /var/log/munin/munin-node.log của bạn cho bất kỳ lỗi nào.
Dưới đây là hình ảnh mẫu của số liệu thống kê.

1.png


2.png

Chúc các bạn thành công.


Nguồn: xenforo.com​
 

Hướng dẫn sử dụng

XenForo 1 XenForo 2
Translate by PVS

Dịch vụ XenForo của VNXF

Mobile/Zalo: 0906081284

Telegram: anhanhxf

Chỉ nhận web nội dung lành mạnh

Nhà Tài Trợ

Mút Xốp Không Gian
Mút Sofa Không Gian
Top Bottom