如何在Debian和Ubuntu上使用vzwatchd监视OpenVZ限制

Vzwatchd是一个OpenVZ监控守护程序,当达到容器的限制时,通过电子邮件通知服务器管理员。 OpenVZ是通常由Web Hosting服务使用的Linux内核虚拟化技术,它是商业虚拟化虚拟化应用程序的免费核心。 OpenVZ是一种轻量级的虚拟化技术,它比KVM或XEN具有更少的开销,它更像是一个Linux LXC监狱,但具有高级限制选项来定义虚拟机可能使用多少资源,并且支持文件系统配额。

本教程介绍了Debian和Ubuntu上vzwatchd守护程序的安装和配置。

1我的虚拟服务器是否使用OpenVZ

您是否从托管公司租用虚拟服务器,而不知道它使用哪些虚拟化技术? 运行以下命令来测试它是否使用OpenVZ:

cat /proc/user_beancounters

如果输出类似于下面的输出,那么您的服务器使用OpenVZ或兼容技术,您可以使用vzwatchd来监视vserver。

root@www:/# cat /proc/user_beancounters
Version: 2.5
uid resource held maxheld barrier limit failcnt
101: kmemsize 190939926 274194432 9223372036854775807 9223372036854775807 0
lockedpages 0 3211 1048576 1048576 0
privvmpages 749006 781311 9223372036854775807 9223372036854775807 0
shmpages 22506 30698 9223372036854775807 9223372036854775807 0
dummy 0 0 9223372036854775807 9223372036854775807 0
numproc 237 312 9223372036854775807 9223372036854775807 0
physpages 486543 804959 0 1048576 0
vmguarpages 0 0 3145728 9223372036854775807 0
oomguarpages 233498 242378 1048576 9223372036854775807 0
numtcpsock 111 298 9223372036854775807 9223372036854775807 0
numflock 253 294 9223372036854775807 9223372036854775807 0
numpty 1 12 9223372036854775807 9223372036854775807 0
numsiginfo 0 33 9223372036854775807 9223372036854775807 0
tcpsndbuf 7083944 11209000 9223372036854775807 9223372036854775807 0
tcprcvbuf 3300832 10792248 9223372036854775807 9223372036854775807 0
othersockbuf 261256 1008400 9223372036854775807 9223372036854775807 0
dgramrcvbuf 0 5152 9223372036854775807 9223372036854775807 0
numothersock 166 526 1024 1024 0
dcachesize 168291899 247843839 9223372036854775807 9223372036854775807 0
numfile 3098 5205 9223372036854775807 9223372036854775807 0
dummy 0 0 9223372036854775807 9223372036854775807 0
dummy 0 0 9223372036854775807 9223372036854775807 0
dummy 0 0 9223372036854775807 9223372036854775807 0
numiptent 28 35 9223372036854775807 9223372036854775807 0

输出显示虚拟机的限制,每行描述一个限制,由vzwatchd监视的列是最后一列,用于计算已达到限制的频率。

2安装vzwatchd

Vzwatchd是用Perl编写的,它使用CPAN命令从Perl CPAN存档下载并安装。

安装先决条件

我将以root用户身份执行以下步骤,在Ubuntu上运行sudo -s成为root:

sudo -s

首先我将安装make工具和nano编辑器,make由CPAN使用构建vzwatchd,我将使用nano以后编辑配置文件:

apt-get install make nano

接下来我将使用以下命令从CPAN安装vzwatchd:

cpan -i App::OpenVZ::BCWatch

如果这是您第一次在服务器上使用CPAN,脚本会询问您有关基本CPAN配置的几个问题:

Would you like to configure as much as possible automatically? [yes]
Would you like me to automatically choose some CPAN mirror sites for you? (This means connecting to the Internet) [yes]

用“是”回答这两个问题。

安装程序现在将下载,编译和安装大量的Perl模块:

root@rz3:~# cpan -i App::OpenVZ::BCWatch

CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] yes

Autoconfigured everything but 'urllist'.

Now you need to choose your CPAN mirror sites. You can let me
pick mirrors for you, you can select them from a list or you
can enter them by hand.

Would you like me to automatically choose some CPAN mirror
sites for you? (This means connecting to the Internet) [yes] yes
Trying to fetch a mirror list from the Internet
Fetching with LWP:
http://www.perl.org/CPAN/MIRRORED.BY

Looking for CPAN mirrors near you (please be patient)
.............................. done!

New urllist
http://www.planet-elektronik.de/CPAN/
http://cpan.noris.de/
http://cpan.lnx.sk/

Autoconfiguration complete.

commit: wrote '/root/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Fetching with LWP:
http://www.planet-elektronik.de/CPAN/authors/01mailrc.txt.gz
Going to read '/root/.cpan/sources/authors/01mailrc.txt.gz'
............................................................................DONE
Fetching with LWP:
http://www.planet-elektronik.de/CPAN/modules/02packages.details.txt.gz
Going to read '/root/.cpan/sources/modules/02packages.details.txt.gz'
Database was generated on Mon, 13 Apr 2015 23:29:02 GMT
..............
New CPAN.pm version (v2.10) available.
[Currently running version is v1.960001]
You might want to try
install CPAN
reload cpan
to both upgrade CPAN.pm and run the new version without leaving
the current session.

[... snip ...]

CPAN.pm: Going to build G/GW/GWOLF/Config-File-1.50.tar.gz

Building Config-File
GWOLF/Config-File-1.50.tar.gz
./Build -- OK
Running Build test
t/pod.t ........... Subroutine main::all_pod_files_ok redefined at /usr/local/share/perl/5.14.2/Test/Pod.pm line 90.
t/pod.t ........... ok
t/pod_coverage.t .. ok
t/test.t .......... 1/11 Invalid characters in key to'be^ignored at line 10 - Ignoring at /root/.cpan/build/Config-File-1.50-NjLxod/blib/lib/Config/File.pm line 41, <GEN0> line 10.
Line format invalid at line 11: 'malformed line that should be also dropped (no equal sign)' at /root/.cpan/build/Config-File-1.50-NjLxod/blib/lib/Config/File.pm line 35, <GEN0> line 11.
t/test.t .......... ok
All tests successful.
Files=3, Tests=13, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.13 cusr 0.02 csys = 0.18 CPU)
Result: PASS
GWOLF/Config-File-1.50.tar.gz
./Build test -- OK
Running Build install
Building Config-File
Installing /usr/local/share/perl/5.14.2/Config/File.pm
Installing /usr/local/man/man3/Config::File.3pm
GWOLF/Config-File-1.50.tar.gz
./Build install -- OK
Running Build for S/SC/SCHUBIGER/App-OpenVZ-BCWatch-0.04.tar.gz
Has already been unwrapped into directory /root/.cpan/build/App-OpenVZ-BCWatch-0.04-4Al97O

CPAN.pm: Going to build S/SC/SCHUBIGER/App-OpenVZ-BCWatch-0.04.tar.gz

Building App-OpenVZ-BCWatch
SCHUBIGER/App-OpenVZ-BCWatch-0.04.tar.gz
./Build -- OK
Running Build test
t/00-load.t ....... ok
t/basic.t ......... ok
t/pod-coverage.t .. ok
t/pod.t ........... ok
All tests successful.
Files=4, Tests=6, 0 wallclock secs ( 0.04 usr 0.01 sys + 0.27 cusr 0.04 csys = 0.36 CPU)
Result: PASS
SCHUBIGER/App-OpenVZ-BCWatch-0.04.tar.gz
./Build test -- OK
Running Build install
Building App-OpenVZ-BCWatch
Installing /usr/local/man/man1/vzwatchd.1p
Installing /usr/local/share/perl/5.14.2/App/OpenVZ/BCWatch.pm
Installing /usr/local/man/man3/App::OpenVZ::BCWatch.3pm
Installing /usr/local/bin/vzwatchd
SCHUBIGER/App-OpenVZ-BCWatch-0.04.tar.gz
./Build install -- OK

重要的是你看到这一行

./Build install -- OK

在编译输出结束时。 如果您收到错误,请重新运行该命令。 我不得不运行命令两次成功地编译所有模块。

要检查安装是否成功,请运行以下命令:

vzwatchd check

这将检查安装并创建一个示例配置文件。

root@server:~# vzwatchd check
/etc/vzwatchd.conf does not exist, creating one with defaults.
Edit /etc/vzwatchd.conf to suit your needs and then start /usr/local/bin/vzwatchd again.

3配置并激活vzwatchd

现在我将编辑vzwatchd.conf文件并设置通知消息的电子邮件地址。

nano /etc/vzwatchd.conf

编辑它后,配置文件应该看起来像这样,只是你自己的电子邮件地址不合适。

mail[from] = [email protected]
mail[to] = [email protected]
mail[subject] = vzwatchd on server.example.com: NOTICE
sleep = 60
verbose = 0
monitor_fields = failcnt
_active = 1

变化是:

  • “mail [from]”行包含通知电子邮件的地址。
  • “mail [to]”行包含收到通知的电子邮件地址。
  • 行“_active”中的值必须更改为1才能激活vzwatchd。
  • 当您运行多个OpenVZ服务器时,可能会方便地更改“mail [subject]”以包含服务器名称。

配置vzwatchd在服务器引导时自动启动:

vzwatchd install
root@server:~# vzwatchd install
+ /usr/sbin/update-rc.d vzwatchd defaults
update-rc.d: warning: /etc/init.d/vzwatchd missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/vzwatchd ...
/etc/rc0.d/K20vzwatchd -> ../init.d/vzwatchd
/etc/rc1.d/K20vzwatchd -> ../init.d/vzwatchd
/etc/rc6.d/K20vzwatchd -> ../init.d/vzwatchd
/etc/rc2.d/S20vzwatchd -> ../init.d/vzwatchd
/etc/rc3.d/S20vzwatchd -> ../init.d/vzwatchd
/etc/rc4.d/S20vzwatchd -> ../init.d/vzwatchd
/etc/rc5.d/S20vzwatchd -> ../init.d/vzwatchd

并启动vzwatchd监视器守护进程:

vzwatchd start
root@server:~# vzwatchd start
Starting /usr/local/bin/vzwatchd server

现在,当您的OpenVZ虚拟服务器达到OpenVZ容器限制之一时,您将收到电子邮件通知。

链接

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏