php script to monitor OTRS queue size from SNMP

最近公司上线OTRS,感觉还不错,顾问居然还给了数据库的结构图,闲下来就写了这个php脚本给Opsview用来监控OTRS支持队列的大小。别问我为什么用php…因为我实在是不会pel….

测试环境:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"

废话不说,先帖脚本:

#!/usr/bin/php
<?php

# Tomas Tang on 18/02/2014
# Check the OTRS support queue size

# Disable error report
error_reporting(0);

# get arguments
$procname = $argv[0];
$queuename = $argv[1];
$warnlevel = $argv[2];
$critilevel = $argv[3];

# keep it for future develop
#$QUEUE = array(
#               "allnew",
#               "escalated",
#               "second_level_support",
#               "first_level_support",
#);


# set query SQL and help
if("$queuename"=='allnew'){
        # all the tickets with state new but not in delete queue
        $sql = "select count(1) from ticket where ticket_state_id=1 and queue_id!=3;";
}elseif("$queuename"=='escalated'){
        # not in use yet, to be finished
        $sql = "select count(1) from ticket where ticket_state_id=1 and queue_id!=3;";
}elseif("$queuename"=='first_level_support'){
        # all tickets which are not (pending to) closed
        $sql = "select count(1) from ticket where ticket_state_id in (1,4,6,11) and queue_id=5;";
}elseif("$queuename"=='second_level_support'){
        # all tickets which are not (pending to) closed
        $sql = "select count(1) from ticket where ticket_state_id in (1,4,6,11) and queue_id=6;";
}else{
        echo "Usage: $procname  <QueueName>  <WarnNumber>  <CriticalNumber>\n";
        echo "      QueueName:      Queueu name to query, right now only in \"allnew\",\"escalated\",\"first_level_support\" and \"second_level_support\"\n";
        echo "      WarnNumber:     Tickets quantity start to be warn, required\n";
        echo "      CriticalNumber: Tickets quantity start to be critical, must greater than WarnNumber, required\n";
        exit(3);
}

# Double check query SQL
if(empty($sql)){
        echo "UNKNOWN - Unknown error\n";
        exit(3);
}

# Check arguments again.
if(!is_numeric("$warnlevel")||!is_numeric("$critilevel")){
        echo "UNKNOWN - Incorrect arguments.\n";
        exit(3);
}elseif($warnlevel>=$critilevel){
        echo "UNKNOWN - Incorrect warn/critical level.\n";
        exit(3);
}

# connect to database
$conn = @mysql_connect("localhost","readonly","readonly") or die("Cannot connect to DB");
mysql_select_db("otrs",$conn);

# fetch data
$result = mysql_query("$sql",$conn) or die("Query failed");
$result = mysql_result($result,0);

# close db
mysql_close($conn);

if ($result>=$critilevel){
        echo "CRITICAL - Queue size is $result, queue is huge!!!\n";
        exit(2);
}elseif($result>=$warnlevel){
        echo "WARN - Queue size is $result, queue is big!\n";
        exit(1);
}else{
        echo "OK - Queue size is $result, queue is OK.\n";
        exit(0);
}

?>

脚本本身没什么好看的,不过最开始用来传参的东西还是蛮好玩的,这种脚本不能跟在web里面一直的传参,只能通过这个叫$argv的数组。

把脚本保存为

/usr/local/bin/check_otrs_queue

修改/etc/snmp/snmpd.conf,加上下面几行

exec otrs_new_tickets /usr/local/bin/check_otrs_queue allnew 15 20
exec first_level_support /usr/local/bin/check_otrs_queue first_level_support 30 50
exec second_level_support /usr/local/bin/check_otrs_queue second_level_support 10 15

最后就是使用check_snmp_exec.sh检测了~~~

Error: /usr/bin/mysqlcheck doesn’t support multiple contradicting commands.

MySQL 5.5对比MySQL 5.0和5.1改变还真不少,就连我以前常用的修复表命令到了5.5时代居然都不能用了。。。

/usr/bin/mysqlcheck --check --auto-repair --optimize --all-databases

一运行就给我报:

Error: /usr/bin/mysqlcheck doesn't support multiple contradicting commands.

而且这个错误在Google里面都好像没有太多的答案,最好在一个阿拉伯文的论坛里面找到了解决方案:其实只要去掉–check就好了

/usr/bin/mysqlcheck --auto-repair --optimize --all-databases

How to use gmail as relay host for postfix in Ubuntu 12.04

Reference: https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

first of all, please note that when the recipient receives your email, the sender will be changed to the gmail account you are going to use

Add the following code in /etc/postfix/main.cf

# Define relay host to gmail
relayhost = smtp.gmail.com:587

# Gmail requires tls connections
smtp_use_tls=yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt #Your trusted CA list

# Gmail requires authentication
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

# define authentication mechanism
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_sasl_tls_security_options = noanonymous

Then, edit your authentication file /etc/postfix/sasl_passwd as the following format:

smtp.gmail.com    USERNAME:PASSWORD

please note that username should be your full mail address, include “@gmail.com” or your own domain name.
then update the file to postfix format for using:

#postmap /etc/postfix/sasl_passwd

remember restart postfix at the last

#service postfix restart

lsiutil to add hot spare drive on line for mpt raid controllers

environment: HP ProLiant ML150 G5 with Ubuntu 8.04 installed

  • Run “mpt-status –i 1” to get the current array information.

    ioc0 vol_id 1 type IM, 2 phy, 231 GB, state DEGRADED, flags ENABLED
    ioc0 phy 0 scsi_id 2 ATA      GB0250C8045      HPG2, 232 GB, state ONLINE, flags NONE
    ioc0 phy 1 scsi_id 9 ATA      GB0250EAFJF      HPG1, 232 GB, state FAILED, flags OUT_OF_SYNC
  • Run “cat /proc/scsi/scsi” to get the available drives information
  • Run “lsiutil”
    • Select the correct port, you can find the information from the 1st step
    • Select 8, and then 16 to confirm the physical drive information, recommended
    • Then select 21 to enter “RAID actions”
    • Select 50 “Create hot spare”
    • You need to figure out the 2 parameters yourselves, I forgot to write down
    • 1st is something like the drive number, you have to compare the information I asked you to collect
    • 2nd is something like the spare group, I suggest you use the same number you use for “mpt-status –i”
    • Then if you select 2 “show physical disks” you will see your hot spare drive
    • Last we run 99 “reset port” to activate the hot spare.
    • Select 0 until you back to shell
  • Run “mpt-status –i 1” again to confirm it is now recovering
    ioc0 vol_id 1 type IM, 2 phy, 231 GB, state DEGRADED, flags ENABLED RESYNC_IN_PROGRESS
    ioc0 phy 2 scsi_id 2 ATA      GB0250C8045      HPG2, 232 GB, state ONLINE, flags NONE
    ioc0 phy 0 scsi_id 3 ATA      GB0250EAFYK      HPG1, 232 GB, state ONLINE, flags OUT_OF_SYNC

Ubuntu 1204 安装NUT监控APC UPS

安装相关程序

apt-get update
apt-get install nut nut-client nut-server

利用udev设定固定的设备文件名

在/etc/udev/rules.d/下面添加52-nut-usb.rules文件,以下是文件内容

# APC - usbhid-ups
SYSFS{idVendor}=="051d", SYSFS{idProduct}=="0002", MODE="664", GROUP="nut", ATTRS{serial}=="ASNNNNNNNNNN", SYMLINK+="ups-test01"

ASNNNNNNNNNN是你的APC UPS序列号,你可以直接在机器上找到,也可以用lsusb -v命令得到(iSerial字段)。

重启udev与ups驱动

udevadm control --reload-rule
udevadm trigger
upsdrvctl start

配置NUT
进入/etc/nut目录。

修改nut.conf文件,将MODE修改为netserver

MODE=netserver

修改ups.conf,在文件最后添加如下内容:

[ups-test01]
        driver = usbhid-ups
        port = /dev/ups-test01
        desc = "UPS-TEST01"
        serial = "ASNNNNNNNNNN"

修改upsd.conf,修改LISTEN字段,把127.0.0.1改为你希望监听的地址,如果监听所有地址,用0.0.0.0

LISTEN 0.0.0.0 3493

修改upsd.users,按如下格式添加用户,这里不详解用户权限了。

[upsclient]
        password = upsclient
        upsmon slave

[upsmaster]
        password = upsmaster
        upsmon master

修改upsmon.conf,添加MONITOR字段

MONITOR ups-test01@localhost 1 upsmaster upsmaster master

重启nut服务之后,ups就已经在监控之中了,你可以用下面的命令查看基本信息了

upsc ups-test01@localhost

添加网页控制页面
直接用命令虽然看的很全面,但是很多时候还是不太方便。nut也提供网页查看方式,设置起来也很方便。

安装相关的软件

apt-get install nut-cgi apache2

修改/etc/nut/hosts.conf,添加如下内容:

MONITOR ups-test01@localhost "UPS-TEST01"

修改apache设置,添加alias到/usr/share/nut下面,然后就可以在网页上查看监控内容了。

Exchange 日常管理:设置邮件转发 [转]

来源: http://blog.sina.com.cn/s/blog_472295620101cxg7.html

场景一:公司有一个Helpdesk的帮助台邮箱,为了方便查看,现在需要设置转发,将发给helpdesk的邮件全部转发到IT系统管理员曾垂鑫的邮箱中。

设置方法如下:

  1. 启动 Exchange 管理控制台。
  2. 在控制台树中,展开“收件人配置”,然后单击“邮箱”。
  3. 搜索helpdesk邮箱,在结果窗格中,右键单击邮箱,然后单击“属性”,如图所示
  4. 1

  5. 在“<邮箱>属性”中的“邮件流设置”选项卡上,单击“传递选项”,然后单击“属性”,如图所示
  6. 2

  7. 单击“转发到”复选框。这将启用“浏览”按钮。如图所示
  8. 3
    注意:如果要将传入邮件传递到邮箱以及所配置的转发地址,请选中“将邮件同时传递到转发地址和原始收件人的邮箱”复选框。如果清除此复选框,则转发所有传入邮件,而不在邮箱中保留副本。

  9. 单击“浏览”打开“选择收件人”对话框。选择要向其转发邮件的收件人,然后单击“确定”。如图所示
  10. 4
    5
    如此设置之后,凡是发往helpdesk的邮件,都将被转发一份副本给曾垂鑫的邮箱。

场景二:曾垂鑫想对其收到的邮件做一个外部邮件系统的副本备份,凡是发给曾垂鑫的邮件都将转发到一个外网的邮箱zengchuixin@126.com中

设置方法如下:

首先我们需要在内部邮件系统为外网的邮箱zengchuixin@126.com增加一个联系人账户。如图所示
6
7
8
9
10
11

然后我们需要修改曾垂鑫邮箱的邮件流设置,首先找到曾垂鑫这个账户,然后选择属性,并切换到邮件流设置选项卡,选择【传递选项】,并点击【属性】,如图所示

12

转发地址设置为我们的外部联系人账户,如图所示

13

然后我们可以发邮件进行测试,让helpdesk给曾垂鑫发一封邮件,可以发现发给曾垂鑫的邮件又被成功地转发到了外网邮箱,外网邮箱成功收到了转发的邮件,如图所示
14

PIN the kernel to the current version in apt

create a new file under /etc/apt/preferences.d with the following content

Package: linux-server
Pin: version 3.2.0.23.25
Pin-Priority: 1001

Package: linux-image-server
Pin: version 3.2.0.23.25
Pin-Priority: 1001

Package: linux-headers-server
Pin: version 3.2.0.23.25
Pin-Priority: 1001

then run

apt-get update
apt-get dist-upgrade

you will find that you won’t get the kernel upgrade notice any more.

you can find you current kernel version with

dpkg -l|grep linux

reference: http://linux.die.net/man/5/apt_preferences

MegaCli命令速查[转]

转自http://hi.baidu.com/ytjwt/blog/item/6d2dfd108c85db70cb80c4e1.html

最近,老是磁盘error,而自己确没有迅速的定位,实在是感到杯具,看到同事们MegaCli查看raid及磁盘错误信息,我这里粘贴下网上别人用的命令,方便自己查阅(另dmesg:显示开机信息,dmidecode,硬件信息)

MegaCli用起来还是很简单的。摘抄一些比较常用的参数如下:

显示适配器个数: MegaCli -adpCount
显示适配器时间: MegaCli -AdpGetTime –aALL
显示所有适配器信息: MegaCli -AdpAllInfo -aAll
显示所有逻辑磁盘组信息: MegaCli -LDInfo -LALL -aAll
显示所有的物理信息: MegaCli -PDList -aAll
查看充电状态: MegaCli -AdpBbuCmd -GetBbuStatus -aALL |grep ‘Charger Status’
显示BBU(后备电池)状态信息: MegaCli -AdpBbuCmd -GetBbuStatus -aALL
显示BBU容量信息: MegaCli -AdpBbuCmd -GetBbuCapacityInfo -aALL
显示BBU设计参数: MegaCli -AdpBbuCmd -GetBbuDesignInfo -aALL
显示当前BBU属性: MegaCli -AdpBbuCmd -GetBbuProperties -aALL
显示Raid卡型号,Raid设置,Disk相关信息: MegaCli -cfgdsply -aALL
查看Cache 策略设置: MegaCli -cfgdsply -aALL |grep Policy
查看充电进度百分比: MegaCli -AdpBbuCmd -GetBbuStatus -aALL |grep ‘Relative State of Charge’

当挂载一磁盘到raid卡上时,是通过fdisk -l查看不了的。我们需要对其做raid,

在线创建raid:

MegaCli -CfgLdAdd -r0 [1:10] WB Direct -a0
MegaCli -CfgLdAdd -r0 [1:11] WB Direct -a0
echo "scsi add-single-device 0 2 1 0" >/proc/scsi/scsi
echo "scsi add-single-device 0 2 2 0" >/proc/scsi/scsi

这样在fdisk -l 就能看到了

RAID 5时,当磁盘坏了,备盘变为online后,如果坏的磁盘已经修好,那么我们需要把修好的盘变为备盘,命令如下:

MegaCli -PDHSP -Set [-EnclAffinity] [-nonRevertible] -PhysDrv[1:5] -a0
# MegaCli -PDHSP -set -PhysDrv[A:B] -aC
A:  it’s Enclosure Number
B: It’s Slot NUmber
C: It’s Array ID2

各种设备和磁盘的不同状态:

Device         |Normal|Damage|Rebuild|Normal
Virtual Drive     |Optimal|Degraded|Degraded|Optimal
Physical Drive     |Online|Failed –> Unconfigured|Rebuild|Online

完整的操作手册在这里:http://www.lsi.com/DistributionSystem/AssetDocument/80-00156-01_RevF.pdf

PS:
关于缓存与电池的操作

强制打开写入缓存(慎用

MegaCli -LDSetProp CachedBadBBU -LALL -aALL
MegaCli -LDSetProp ForcedWB -LALL -aALL

设定没有电池就不缓存:

MegaCli -LDSetProp WB -LALL -aALL
MegaCli -LDSetProp NoCachedBadBBU -LALL -aALL