Feb 28
留档备查

脚本里设置了 ServerActive ,会主动尝试到zabbix server注册,但需要先在zabbix frontend的 configuration->actions->auto registration 配置好 add host 动作,这样才会自动添加。

引用

#!/bin/bash

set -x

ZABBIX_SERVER=192.168.1.100

wget http://repo.zabbix.com/zabbix/3.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.2-1+xenial_all.deb
dpkg -i zabbix-release_3.2-1+xenial_all.deb
apt update
apt -y install zabbix-agent

ip=`ifconfig  | grep -o 'inet addr:172\.[0-9.]*' | awk -F: '{print $2}'`
sed -i \
    -e 's/^Server=.*$/Server='$ZABBIX_SERVER'/' \
    -e 's/^ServerActive=.*$/ServerActive=lan.zabbix.thebitplus.com/' \
    -e 's/^Hostname=.*$/Hostname='`hostname`'/' \
    /etc/zabbix/zabbix_agentd.conf

sudo update-rc.d zabbix-agent enable
sudo service zabbix-agent restart

echo "Done"
Feb 11
莫名其妙的一个错误,手头的两个sentry实例里都没有这个表,但是还是会报这个错。

没研究具体的代码,但是通过查找源码里的 sentry_email 发掘了表结构,建表并授权即可:
引用
$ psql -h $HOST -U root -W sentry
sentry=> create extension citext;
CREATE EXTENSION
sentry=> create table sentry_email (id bigserial primary key, email CITEXT, date_added timestamp with time zone);
CREATE TABLE
sentry=> grant all privileges on table sentry_email to sentry;
GRANT
sentry=> GRANT USAGE, SELECT ON SEQUENCE sentry_email_id_seq1 to sentry;
GRANT
Feb 1

Bash:传递命令行参数 不指定

felix021 @ 2018-2-1 14:18 [IT » shell] 评论(1) , 引用(0) , 阅读(10290) | Via 本站原创
不知道为什么这些年写脚本一直没有解决这个小需求:把命令行参数传递给脚本中的某个命令继续执行

例如我想写一个 colored-echo 命令:
引用
#!/bin/bash

color=$1
shift

echo -ne "\x1b[$color"

echo -n $1 $2 $3 $4 $5

echo -e "\x1b[0m"


然后这么调用
引用
colored-echo 41m hello world


但参数的数量是可变的,另一种实现是
引用
cmd=echo

for ((i=1; i<=$#; i++))
do
  cmd="cmd ${@:i:1}"
done

$cmd


但个实现也很奇怪,如果某个参数里面出现了引号或者空格,会因为bash奇葩的转义逻辑导致跟预期不一致。

可能因为痛感不强烈,所以拖了几年也没真正花心思去解决它,今天搜了一会,总算找到靠谱的解决方案了,而且超级简单:
引用
echo "$@"
Feb 1

墙外最老牌云数据库测试 不指定

felix021 @ 2018-2-1 00:03 [IT » ] 评论(0) , 引用(0) , 阅读(2272) | Via 本站原创
平台:AWS Aurora (MySQL 5.6.10a 兼容版本)

测试软件:sysbench 0.5 --test=oltp.lua (与之前一样的测试脚本

DB配置:db.r4.large (2核,约16GB RAM),Aurora Shared Disk

sysbench配置:m4.xlarge(4核,16GB RAM),20GB+100GB EBS(300/3000 IOPS)

测试结果:

tps: 276 tps
reads: 6400 tps
writes: 1100 tps
response time: 130ms (95%)

测试结果:
引用

[  10s] threads: 32, tps: 174.89, reads: 4077.58, writes: 705.68, response time: 305.44ms (95%), errors: 0.00, reconnects:  0.00
[  20s] threads: 32, tps: 230.00, reads: 5302.10, writes: 916.00, response time: 236.40ms (95%), errors: 0.00, reconnects:  0.00
[  30s] threads: 32, tps: 248.90, reads: 5680.20, writes: 994.70, response time: 219.35ms (95%), errors: 0.00, reconnects:  0.00
[  40s] threads: 32, tps: 250.10, reads: 5776.60, writes: 1000.00, response time: 219.16ms (95%), errors: 0.00, reconnects:  0.00
[  50s] threads: 32, tps: 253.20, reads: 5811.50, writes: 1012.00, response time: 205.62ms (95%), errors: 0.00, reconnects:  0.00
[  60s] threads: 32, tps: 252.30, reads: 5832.70, writes: 1009.60, response time: 197.89ms (95%), errors: 0.00, reconnects:  0.00
...
...
[1780s] threads: 32, tps: 279.00, reads: 6403.60, writes: 1115.30, response time: 135.88ms (95%), errors: 0.00, reconnects:  0.00
[1790s] threads: 32, tps: 276.30, reads: 6391.30, writes: 1106.80, response time: 155.70ms (95%), errors: 0.00, reconnects:  0.00
[1800s] threads: 32, tps: 276.90, reads: 6343.60, writes: 1106.80, response time: 131.83ms (95%), errors: 0.00, reconnects:  0.00
OLTP test statistics:
    queries performed:
        read:                            11371476
        write:                          1977648
        other:                          988824
        total:                          14337948
    transactions:                        494412 (274.66 per sec.)
    read/write requests:                13349124 (7415.78 per sec.)
    other operations:                    988824 (549.32 per sec.)
    ignored errors:                      0      (0.00 per sec.)
    reconnects:                          0      (0.00 per sec.)

General statistics:
    total time:                          1800.0982s
    total number of events:              494412
    total time taken by event execution: 57593.8148s
    response time:
        min:                                24.28ms
        avg:                                116.49ms
        max:                                505.90ms
        approx.  95 percentile:            142.84ms

Threads fairness:
    events (avg/stddev):          15450.3750/15.24
    execution time (avg/stddev):  1799.8067/0.02
Jan 27
AWS的http/https负载均衡挺好用的,但是有一点比较麻烦,因为是应用层协议,所以在后端的nginx(以及下面挂的php-fpm)看到的 REMOTE_ADDR是负载均衡的IP,而直连LB的IP,则是保存在了 HTTP_X_FORWARD_FOR 这个header里面。

当然,在应用里面增加一小段代码去解析这个header也不是什么难事,但是毕竟有些框架已经有一套解析的方案了,而且碰到客户端自己还用代理的时候,这个字段的value是一串IP列表(直连负载均衡的ip是最后一个),就变得更复杂了。比如:

$ curl https://test.com/ -H "X-FORWARDED-FOR: 8.8.8.8"

php记录下来的 $_SERVER 变量就长这样了:

array (
  'HTTP_X_FORWARDED_PORT' => '443',
  'HTTP_X_FORWARDED_PROTO' => 'https',
  'HTTP_X_FORWARDED_FOR' => '8.8.8.8, 13.31.11.23',
  'SERVER_PORT' => '80',  'SERVER_ADDR' => '172.24.22.33',
  'REMOTE_PORT' => '56247',
  'REMOTE_ADDR' => '172.24.22.34',
)

还是有点头疼的。幸好nginx有提供一个 ngx_http_realip_module 模块,可以解决这个问题,只要在配置里加上这么两行:

set_real_ip_from 172.24.0.0/16; #注:这个ip端是负载均衡所处VPC的CIDR
real_ip_header X-Forwarded-For;

重启nginx以后,再次访问就可以看到,REMOTE_ADDR 变成了 HTTP_X_FORWARDED_FOR 的 IP列表里最后一个IP。
Sep 22
(瞅一眼才发现四个月没写了,确实是好久没写代码了,没啥心得,不过想想好像可以写个提纲凑个数)

我们的业务主要还是用 MySQL 存储业务数据。

MySQL 一个很麻烦的问题是,alter table 的时候往往要锁表,而业务在最初设计的时候,又没法为未来的所有改动预留合适的字段,结果就是,要么另外建一张表横向扩展,要么熬到半夜,忍受锁表带来的业务中断;不过在多次实践中还是有一些心得体会,可以简单列一下。

1. alter table 是否都会锁表?

不都会,有些情况可以不锁表,例如,修改默认值,或者对 enum 类型字段增加一个 value

2. 对 enum 类型字段加 value 就不会锁表吗?

不一定,如果新增的 value 是最后一个就不会锁表,但也要注意,还是有坑(不能超过当前的bit数能表示的最大值)(为什么?)

3. 有没办法即 alter table 但又不长时间锁表?

有,percona-toolkit 有个工具能做到,原理很简单,新建一个表A的副本A',在A'上加字段,并同步数据,最后用一个 alter 语句对换两张表,但据说有BUG

4. 安利一下与 MySQL 协议基本兼容的 TiDB ,可以直接在线不锁表 alter table
May 26

centos 安装 daemontools 不指定

felix021 @ 2017-5-26 00:10 [随想] 评论(0) , 引用(0) , 阅读(10282) | Via 本站原创
09年在B公司实习的时候从运维同学偷师知道了daemontools这个package,有个supervise命令特别好用(很符合B公司当时宣传的“简单可依赖”),以前用ubuntu/debian,apt-get直接就装上了。现在线上用的是centos,需要自己编译安装,稍微麻烦点。具体的过程在官网 http://cr.yp.to/daemontools/install.html 说得还是比较详细了,不过编译的时候可能会报错
引用
./load envdir unix.a byte.a
/usr/bin/ld: errno: TLS definition in /lib64/libc.so.6 section .tbss mismatches non-TLS reference in envdir.o
/lib64/libc.so.6: could not read symbols: Bad value
collect2: error: ld returned 1 exit status

搜了一下,解决方案也很简单:
引用
The fix is quite simple: explicitly add #include <errno.h> to the offending file and then recompile.

按照说明,在 src/error.h 里面添加了这句再编译就通过了。
May 25
$ vi /config/environments/production.rb

注释掉 “config.action_mailer.delivery_method = :sendmail”,

并在下面添加
引用

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.smtp_settings = {
      :address              => "smtp.exmail.qq.com",
      :port                => 465,
      :domain              => 'yourdomain.com',
      :user_name            => '发信帐号',
      :password            => '密码',
      :authentication      =>  'login',
      :enable_starttls_auto => true,
      :tls                  => true,
      :email_from          => '发信帐号'
  }
分页: 9/103 第一页 上页 4 5 6 7 8 9 10 11 12 13 下页 最后页 [ 显示模式: 摘要 | 列表 ]