ซึ่งเทคนิคนี้ เอกสารของทาง Microsoft ก็ได้ระบุเอาไว้ ดังนี้
If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.
เพื่อให้เห็นภาพที่ชัดขึ้น ผมจะใช้การวนลูปเข้ามาช่วยเพื่อให้เห็นการทำงานของ CPU ที่เป็นจริงมากยิ่งขึ้น ดังโค้ดตัวอย่าง
โดยผลลัพธ์จากโค้ดด้านบน ก็จะแสดงการทำงานของ CPU ทุก 1 วินาทีเป็นจำนวน 10 ครั้ง
จะเห็นว่าการดึงข้อมูลการใช้งาน CPU ไม่ได้ยุ่งยาก แค่มีทริคนิดเดียวตรงที่จะต้องมีการเรียก NextValue() 2 ครั้ง และต้องมีการหน่วงเวลาก่อนการเรียกครั้งที่ 2 ก็จะได้ผลลัพธ์ที่ถูกต้อง
ในช่วงที่ผ่านมาผู้เขียนได้รับมอบหมายให้พัฒนาเครื่องมือสำหรับมอนิเตอร์การใช้งานทรัพยากรต่างๆ บน Server ที่ให้บริการลูกค้า ทั้งในส่วน Memory CPU และ Disk โดยโจทย์คือพัฒนา Service ขึ้นมาตัวนึงด้วยเครื่องมือที่เราใช้งานกันอยู่แล้วคือ Visual Studio.NET ด้วยภาษ C# สำหรับอ่านทรัพยากรต่างๆ บน Server แล้วจึงเขียนค่าเหล่านั้นลงไฟล์ จากนั้นจะมีเครื่องมืออีกตัวเข้ามาอ่านไฟล์ดังกล่าวแล้วนำไปแสดงผลเป็นกราฟในรูปแบบที่ต้องการต่อไป
แต่ด้วยข้อจำกัด เราไม่สามารถหาหน่วยความจำทั้งหมดจาก PerformanceCounter โดยจะต้องดึงข้อมูลผ่านวิธีการอื่น นั่นคือการเรียกผ่าน Windows API Function และต้องประกาศ Structure เพิ่มเติมเพื่อให้รองรับข้อมูลที่ Windows API ส่งกลับมา ดังนี้
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
}
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
Received: from [154.117.164.59] (unknown [154.117.164.59]) by mailscan.in.psu.ac.th (Postfix) with ESMTP id 69F2B150768 for <kanakorn.h@psu.ac.th>; Thu, 5 Mar 2020 13:24:42 +0700 (ICT)
แล้วจึงส่งเข้าระบบ PSU Email
Received: from mailscan.in.psu.ac.th (unknown [192.168.107.12]) by mail.psu.ac.th (Postfix) with ESMTP id A034D464FC7 for <kanakorn.h@psu.ac.th>; Thu, 5 Mar 2020 13:24:46 +0700 (+07)
แล้วจึงเข้า Mailbox ของ PSU (ข้อมูล version ของ cyrus เอาออกไม่ได้จริง ๆ ครับ ไว้รอ Upgrade)
Received: from mail.psu.ac.th ([unix socket]) by mail (Cyrus v2.4.18-Debian-2.4.18-3) with LMTPA; Thu, 05 Mar 2020 13:24:46 +0700
จะเห็นได้ว่า ต้นทางคือ IP Address : 154.117.164.59
จากนั้น เอาไปสร้าง Visualization แบบ Vertical Bar กำหนดให้ Y Axis เป็นจำนวน X Axis เป็น Username โดยที่ Group by geoip.country_name และ description ก็จะทำให้รู้ว่า ใครบ้างที่ มีการใช้งานนอกประเทศ และ เป็นการใช้งานแบบไหน
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
แก้ไขไฟล์ nginx.conf
sudo vim /etc/nginx/sites-available/default
ตั้งค่า fastcgi_pass ให้เรียกใช้งาน php7.0-fpm
[...]
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ { include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock; }
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[...]
จากนั้นทำการสร้าง DB และกำหนด User, Password ที่ใช้ในการเข้าถึง (piwikuser@ ให้แทนที่ด้วยรหัสผ่าน)
create database piwikdb;create user piwikuser@localhost identified by 'piwikuser@';grant all privileges on piwikdb.* to piwikuser@localhost identified by 'piwikuser@';flush privileges;quit;
เมื่อทำการตั้งค่าอีเมล์เรียบร้อยแล้ว เราสามารถให้ส่งรายงานสรุป Per Site ให้ Admin แต่ละ site ตรวจสอบได้รายวัน และเมื่อมี Version ใหม่ ก็จะมีเมล์แจ้งเช่นกัน โดยวิธีการ Update จะสามารถทำผ่านหน้า Web แต่สุดท้ายจะมีให้รันคำสั่งบน Command Line เพื่อ Upgrade Database แต่จะมีวิธีทำอธิบายไว้ละเอียดอยู่แล้วครับ ขอให้สนุกกับการใช้งานครับปล. ผมข้ามขั้นตอนการตั้งค่า SSL[4] ซึ่งแนะนำให้ตั้งค่าด้วยครับ