Fork me on GitHub

SRS使用小记

SRS简介

互联网上的两种主要的分发方式:HLS和RTMP。
SRS支持HLS/RTMP两种成熟而且广泛应用的流媒体分发方式。

HLS指Apple的HLS(Http Live Streaming),本身就是Live(直播)的,不过Vod(点播)也能支持。HLS是Apple平台的标准流媒体协议,和RTMP在PC上一样支持得天衣无缝。

SRS直播将RTMP作为基本协议,以各种方式转码为RTMP后输入到SRS,输出为RTMP和HLS,支持广泛的客户端和各种应用场景。
SRS点播还在计划中,不会使用RTMP作为点播协议,点播还是文件为主,即HTTP协议为主。

SRS编译配置参数详解
https://github.com/ossrs/srs/wiki/v2_CN_Build

获取SRS

git clone https://github.com/ossrs/srs

编译SRS

./configure && make -j4

启动SRS

./objs/srs -c conf/srs.conf
cp etc/init.d/srs /etc/init.d/srs
注意修改配置文件
CONFIG="./conf/srs.conf"

配置文件说明

SRS的使用,主要是对配置文件的理解

conf/srs.conf
# main config for srs.
# @see full.conf for detail config.
listen 1935; #监听端口
max_connections 1000;
srs_log_tank file;
srs_log_file ./objs/srs.log;
http_api {
enabled on;
listen 1985;
}
http_server {
enabled on;
listen 8085;
dir ./objs/nginx/html;
}
stats {
network 0;
disk sda sdb xvda xvdb;
}
vhost __defaultVhost__ {
}
---
RTMP Cluster配置
conf/edge.conf
# the config for srs origin-edge cluster
# @see https://github.com/ossrs/srs/wiki/v1_CN_Edge
# @see full.conf for detail config.
listen 19350; #边缘节点监听端口
max_connections 1000;
pid objs/edge.pid;
daemon off;
srs_log_tank console;
vhost __defaultVhost__ {
mode remote;
origin 127.0.0.1:1935; #源节点监听端口
}
---
HTTP FLV Live Stream配置
在vhost下添加http_remux
vhost __defaultVhost__ {
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
hstrs on;
}
}
播放流地址
HTTP FLV: http://192.168.1.158:8085/live/ljd.flv
https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHttpStream#http-live-stream-config
---
HTTP FLV Live Stream Cluster配置
单独节点,区别于在vhost下添加
vhost __defaultVhost__ {
mode remote; # 边缘节点开启
origin 192.168.1.158; #源节点地址
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
hstrs on;
}
---
HLS分发配置
在vhost下添加hls
vhost __defaultVhost__ {
hls {
enabled on;
hls_fragment 10;
hls_window 60;
hls_path ./objs/nginx/html;
hls_m3u8_file [app]/[stream].m3u8;
hls_ts_file [app]/[stream]-[seq].ts;
}
}
HLS流地址为: http://192.168.1.158/live/ljd.m3u8
---
转码HLS后分发
HLS需要h.264+aac,若不符合这个要求则需要转码。
在vhost下添加transcode 转码
vhost __defaultVhost__ {
...
#低延时配置
gop_cache off;
queue_length 10;
min_latency on;
mr {
enabled off;
}
mw_latency 100;
tcp_nodelay on;
#转码HLS
transcode live{
# whether the transcode enabled.
# if off, donot transcode.
# default: off.
enabled on;
# the ffmpeg
ffmpeg /usr/local/ffmpeg/bin/ffmpeg;
# the transcode engine for matched stream.
# all matched stream will transcoded to the following stream.
# the transcode set name(ie. hd) is optional and not used.
engine ff {
# whether the engine is enabled
# default: off.
enabled on;
# input format, can be:
# off, do not specifies the format, ffmpeg will guess it.
# flv, for flv or RTMP stream.
# other format, for example, mp4/aac whatever.
# default: flv
iformat flv;
# video encoder name. can be:
# libx264: use h.264(libx264) video encoder.
# copy: donot encoder the video stream, copy it.
# vn: disable video output.
vcodec libx264;
# video bitrate, in kbps
# @remark 0 to use source video bitrate.
# default: 0
vbitrate 1500;
# video framerate.
# @remark 0 to use source video fps.
# default: 0
vfps 0;
# video width, must be even numbers.
# @remark 0 to use source video width.
# default: 0
vwidth 1440;
# video height, must be even numbers.
# @remark 0 to use source video height.
# default: 0
vheight 720;
# the max threads for ffmpeg to used.
# default: 1
vthreads 12;
# x264 profile, @see x264 -help, can be:
# high,main,baseline
vprofile main;
# x264 preset, @see x264 -help, can be:
# ultrafast,superfast,veryfast,faster,fast
# medium,slow,slower,veryslow,placebo
vpreset medium;
# other x264 or ffmpeg video params
vparams {
# ffmpeg options, @see: http://ffmpeg.org/ffmpeg.html
t 100;
# 264 params, @see: http://ffmpeg.org/ffmpeg-codecs.html#libx264
coder 1;
b_strategy 2;
bf 3;
refs 10;
}
# audio encoder name. can be:
# libfdk_aac: use aac(libfdk_aac) audio encoder.
# copy: donot encoder the audio stream, copy it.
# an: disable audio output.
acodec copy;
# audio bitrate, in kbps. [16, 72] for libfdk_aac.
# @remark 0 to use source audio bitrate.
# default: 0
abitrate 70;
# audio sample rate. for flv/rtmp, it must be:
# 44100,22050,11025,5512
# @remark 0 to use source audio sample rate.
# default: 0
asample_rate 44100;
# audio channel, 1 for mono, 2 for stereo.
# @remark 0 to use source audio channels.
# default: 0
achannels 2;
# other ffmpeg audio params
aparams {
# audio params, @see: http://ffmpeg.org/ffmpeg-codecs.html#Audio-Encoders
# @remark SRS supported aac profile for HLS is: aac_low, aac_he, aac_he_v2
profile:a aac_low;
bsf:a aac_adtstoasc;
}
# output format, can be:
# off, do not specifies the format, ffmpeg will guess it.
# flv, for flv or RTMP stream.
# other format, for example, mp4/aac whatever.
# default: flv
oformat flv;
# output stream. variables:
# [vhost] the input stream vhost.
# [port] the intput stream port.
# [app] the input stream app.
# [stream] the input stream name.
# [engine] the tanscode engine name.
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}
转码后的RTMP流地址为:rtmp://192.168.1.158/live/ljd_ff
转码后的HLS流地址为: http://192.168.1.158/live/ljd_ff.m3u8
---
SRS支持将RTMP流录制成flv文件
vhost __defaultVhost__ {
dvr {
# whether enabled dvr features
# default: off
enabled on;
# the dvr output path.
# we supports some variables to generate the filename.
# [vhost], the vhost of stream.
# [app], the app of stream.
# [stream], the stream name of stream.
# [2006], replace this const to current year.
# [01], replace this const to current month.
# [02], replace this const to current date.
# [15], replace this const to current hour.
# [04], repleace this const to current minute.
# [05], repleace this const to current second.
# [999], repleace this const to current millisecond.
# [timestamp],replace this const to current UNIX timestamp in ms.
# @remark we use golang time format "2006-01-02 15:04:05.999"
# for example, for url rtmp://ossrs.net/live/livestream and time 2015-01-03 10:57:30.776
# 1. No variables, the rule of SRS1.0(auto add [stream].[timestamp].flv as filename):
# dvr_path ./objs/nginx/html;
# =>
# dvr_path ./objs/nginx/html/live/livestream.1420254068776.flv;
# 2. Use stream and date as dir name, time as filename:
# dvr_path /data/[vhost]/[app]/[stream]/[2006]/[01]/[02]/[15].[04].[05].[999].flv;
# =>
# dvr_path /data/ossrs.net/live/livestream/2015/01/03/10.57.30.776.flv;
# 3. Use stream and year/month as dir name, date and time as filename:
# dvr_path /data/[vhost]/[app]/[stream]/[2006]/[01]/[02]-[15].[04].[05].[999].flv;
# =>
# dvr_path /data/ossrs.net/live/livestream/2015/01/03-10.57.30.776.flv;
# 4. Use vhost/app and year/month as dir name, stream/date/time as filename:
# dvr_path /data/[vhost]/[app]/[2006]/[01]/[stream]-[02]-[15].[04].[05].[999].flv;
# =>
# dvr_path /data/ossrs.net/live/2015/01/livestream-03-10.57.30.776.flv;
# @see https://github.com/ossrs/srs/wiki/v2_CN_DVR#custom-path
# @see https://github.com/ossrs/srs/wiki/v2_CN_DVR#custom-path
# default: ./objs/nginx/html/[app]/[stream].[timestamp].flv
dvr_path ./objs/nginx/html/[app]/[stream].[timestamp].flv;
# the dvr plan. canbe:
# session reap flv when session end(unpublish).
# segment reap flv when flv duration exceed the specified dvr_duration.
# default: session
dvr_plan session;
# the param for plan(segment), in seconds.
# default: 30
dvr_duration 30;
# the param for plan(segment),
# whether wait keyframe to reap segment,
# if off, reap segment when duration exceed the dvr_duration,
# if on, reap segment when duration exceed and got keyframe.
# default: on
dvr_wait_keyframe on;
# about the stream monotonically increasing:
# 1. video timestamp is monotonically increasing,
# 2. audio timestamp is monotonically increasing,
# 3. video and audio timestamp is interleaved monotonically increasing.
# it's specified by RTMP specification, @see 3. Byte Order, Alignment, and Time Format
# however, some encoder cannot provides this feature, please set this to off to ignore time jitter.
# the time jitter algorithm:
# 1. full, to ensure stream start at zero, and ensure stream monotonically increasing.
# 2. zero, only ensure sttream start at zero, ignore timestamp jitter.
# 3. off, disable the time jitter algorithm, like atc.
# default: full
time_jitter full;
}
}

Nginx/Tomcat添加Https支持

安装certbot

https://certbot.eff.org/

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

nginx生成免费证书

./certbot-auto certonly --webroot -w /web/ss-panel/public -d ss.wiredtiger.org

nginx生成dhparams

openssl dhparam -out /etc/ssl/certs/dhparams.pem 2048

nginx配置添加

server {
#强制跳转https
listen 80;
server_name ss.wiredtiger.org;
return 301 https://$server_name$request_uri;
}
server_name ss.wiredtiger.org;
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/ss.wiredtiger.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ss.wiredtiger.org/privkey.pem;
ssl_dhparam /etc/ssl/certs/dhparams.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-Xss-Protection 1;

tomcat生成免费证书

./certbot-auto certonly --standalone -m lijd@rgbvr.com -d test.wiredtiger.org
生成.p12文件
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out fullchain_and_key.p12 -name tomcat
admin123
.jks证书
keytool -importkeystore -deststorepass admin123 -destkeypass admin123 -destkeystore rgbvrkeystore.jks -srckeystore fullchain_and_key.p12 -srcstoretype PKCS12 -srcstorepass rgbvradmin -alias tomcat

tomcat配置修改

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="conf/rgbvrkeystore.jks"
keystorePass="admin123"
keyAlias="tomcat"
keyPass="admin123"
/>

证书更新

./certbot-auto renew

SSL Server测试

https://www.ssllabs.com/ssltest/index.html

从日志恢复数据

循环遍历日志

stat.txt
2016-11-09 00:00:23 "deviceId":"F9D4F729-7EE7-467F-BF3F-7D9D008EAB" "userId":"20048603"
2016-11-09 00:00:29 "deviceId":"c96996784f99e106c32138940da2987c" "osSystem":"Android6.0" "userId":"20066192"

过滤出无用字段(类似循环处理)

grep -i -v "osSystem" stat1.txt >> stat11.txt
grep -i "osSystem" stat1.txt >> stat12.txt

处理成mysql可以load的格式

vi 列编辑处理失败。
2016-11-08 23:38:53 ,777b102b6c5cdd1ca087b269e8930be2,20066135
2016-11-08 23:38:56 ,54bac2b7794f7567b1e254bf805e3f7a,
awk -F'"' '{OFS=","; print $1,$4,$8}'
过滤user_id为空的字段
awk -F',' '{if($3>0) {OFS=","; print $1,$2,$3}}' test1.txt

创建表并load数据。

CREATE TABLE user_device(
`create_time` DATETIME NOT NULL,
`user_id` BIGINT(20) NOT NULL,
`device_id` VARCHAR(40) DEFAULT NULL
);
load data infile '/home/res2.txt' into table user_device FIELDS TERMINATED BY ',';

ffmpeg 编译安装

系统环境

Ubuntu 14.04.3 LTS

最近测试ffmpeg转推,使用老版本ffmpeg流转推一段时间会断掉,测试发现在新版本ffmpeg效果明显好很多。但是由于依赖众多(主要问题),编译花费了好长时间。
记录下编译过程。

ffmpeg 编译安装

依赖安装

apt-get install yasm pkg-config
apt-get install gnutls-bin libgnutls-dev libass-dev libfdk-aac-dev
libmp3lame0 libmp3lame-dev libpulse0 libpulse-dev libsoxr0 libsoxr-dev speex libspeex-dev libopus0 libopus-dev libvpx1 libvpx-dev
libwavpack1 libwavpack-dev
apt-get install libx264-dev libxvidcore-dev libopencore-amrwb-dev libopencore-amrnb-dev libfaad-dev libfaac-dev libmp3lame-dev libtwolame-dev liba52-0.7.4-dev libcddb2-dev libcdaudio-dev libcdio-cdda-dev libvorbis-dev libopenjpeg-dev
add-apt-repository ppa:sunab/kdenlive-release
apt-get install libvidstab1.0 libvidstab-dev

x265 安装

# ubuntu packages:
sudo apt-get install mercurial cmake cmake-curses-gui build-essential yasm
Note: if the packaged yasm is older than 1.2, you must download yasm (1.3 recommended) and build ithg clone https://bitbucket.org/
hg clone https://bitbucket.org/multicoreware/x265
cd x265/build/linux
./make-Makefiles.bash
make
make install

编译安装

./configure --prefix=/opt/ffmpeg --extra-libs=-ldl --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab --enable-libwavpack --enable-nvenc
make && make install
添加环境变量或做软连接
查看ffmpeg版本
ffmpeg -version
ffmpeg version N-82143-gbf14393 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
configuration: --prefix=/opt/ffmpeg --extra-libs=-ldl --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab --enable-libwavpack --enable-nvenc
libavutil 55. 35.100 / 55. 35.100
libavcodec 57. 65.100 / 57. 65.100
libavformat 57. 57.100 / 57. 57.100
libavdevice 57. 2.100 / 57. 2.100
libavfilter 6. 66.100 / 6. 66.100
libavresample 3. 2. 0 / 3. 2. 0
libswscale 4. 3.100 / 4. 3.100
libswresample 2. 4.100 / 2. 4.100
libpostproc 54. 2.100 / 54. 2.100

祝你成功!

nginx 负载均衡配置

nginx version: nginx/1.10.1
负载机器:192.168.1.168 80
upstream机器:192.168.1.169:81
192.168.1.170:81

nginx安装

wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
#For Ubuntu 14.04 replace codename to trusty
deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx
apt-get update
apt-get install nginx

nginx负载配置

upstream haha {
server 192.168.1.169:81;
server 192.168.1.170:81;
}
server {
listen 80;
server_name www.haha.com;
location / {
proxy_pass http://haha;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log /data1/logs/nginx/haha.log;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

后端server配置

server{
server_name 192.168.1.169;
listen 81;
root /web/haha/;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#location ~ /\.ht {
#deny all;
#}
}

测试

在后端server /web/haha/下分别写多个测试页面。

grafana influxdb构建自定义监控

grafana 安装

$ wget https://grafanarel.s3.amazonaws.com/builds/grafana_3.0.4-1464167696_amd64.deb
$ sudo apt-get install -y adduser libfontconfig
$ sudo dpkg -i grafana_3.0.4-1464167696_amd64.deb
/etc/init.d/grafana-server start/stop/status
http://192.168.1.158:3000/ admin/admin

influxdb 安装

wget https://dl.influxdata.com/influxdb/releases/influxdb_1.0.2_amd64.deb
sudo dpkg -i influxdb_1.0.2_amd64.deb
web访问influxdb http://192.168.1.158:8083/

influxdb 基本操作

show databases;
show MEASUREMENTS;
show SERIES;
create database mydb;
insert s_status,prog='http://www.wiredtiger.org/abc',server_ip=192.168.1.168,client_ip=192.168.1.169 stream_rate=3047,recv_speed=3123,duration_time=761
select * from /.*/

python 操作influxdb

Install, upgrade and uninstall InfluxDB-Python with these commands:
pip install influxdb
pip install --upgrade influxdb
pip uninstall influxdb
git clone https://github.com/influxdata/influxdb-python.git
cd influxdb-python/
pip install -r requirements.txt -i http://pypi.douban.com/simple
python setup.py install

写入数据

$ python
>>> from influxdb import InfluxDBClient
>>> json_body = [
{
"measurement": "cpu_load_short",
"tags": {
"host": "server01",
"region": "us-west"
},
"time": "2009-11-10T23:00:00Z",
"fields": {
"value": 0.64
}
}
]
>>> client = InfluxDBClient('localhost', 8086, 'root', 'root', 'example')
>>> client.write_points(json_body)
>>> result = client.query('select value from cpu_load_short;')
>>> print("Result: {0}".format(result))

grafana 配置数据源显示

配置数据源
数据源
最终显示效果
数据显示

notes:
http://docs.grafana.org/v3.0/installation/debian/
http://influxdb-python.readthedocs.io/en/latest/include-readme.html

Percona Toolkit使用小结

Percona Toolkit安装

1、下载包
2、安装

perl Makefile.PL
make
make test
make install

pt-online-schema-change 在线修改表结构

修改表结构前,切记备份!!!
pt-osc原理:
1、创建一个和要执行 alter 操作的表一样的新的空表结构(是alter之前的结构)
2、在新表执行alter table 语句(速度应该很快)
3、在原表中创建触发器3个触发器分别对应insert,update,delete操作
4、以一定块大小从原表拷贝数据到临时表,拷贝过程中通过原表上的触发器在原表进行的写操作都会更新到新建的临时表
5、Rename 原表到old表中,在把临时表Rename为原表
6、如果有参考该表的外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理
7、默认最后将旧原表删除

添加列

pt-online-schema-change h=192.168.1.87,u=ljd,D=mytest,t=a --ask-pass \
--alter "add column c varchar(10)" --print --dry-run
pt-online-schema-change h=192.168.1.87,u=ljd,D=mytest,t=a --ask-pass \
--alter "add column c varchar(10)" --print --execute

删除索引

pt-online-schema-change h=192.168.1.87,u=rgbvr,D=mytest,t=s_event_notice --ask-pass \
--alter "DROP INDEX notice_id_UNIQUE" \
--print --execute

为什么要去除重复的索引

1、多余的索引占用磁盘空间,会引起不必要的磁盘IO
2、多余的索引会导致数据库在进行索引选择的时候变慢,
尤其是索引越多的时候越突出(主要是相关联的索引才会影响索引选择)
3、重复的索引会导致表的更新变慢

pt-duplicate-key-checker 检测MySQL冗余和重复索引

pt-duplicate-key-checker h=192.168.1.87,u=ljd -d mytest --ask-pass --nocluster

pt-kill 杀掉进程

由于空闲连接较多导致超过最大连接数或者某个有问题的sql导致mysql负载很高时,可以使用pk-kill杀掉进程。
例如:每10秒检查一次,杀死指定用户超过100秒的查询

pt-kill --no-version-check h=192.168.1.87,u=ljd,P=3306,p=root123 \
--match-user root --victims all --busy-time 10 \
--interval 10 --kill --print
参数说明:
match-command
指定杀死的查询类型
match-user
指定杀死的用户名,即杀死该用户的查询
busy-time
指定杀死超过多少秒的查询
kill
执行kill命令
victims
表示从匹配的结果中选择,类似SQL中的where部分,all是全部的查询
interal
每隔多少秒检查一次

notes:
1、使用–ask-pass 提示错误Can’t locate Term/ReadKey.pm in @INC

apt-get install libterm-readkey-perl

Smokeping nginx 安装小记

nginx支持cgi

aptitude install fcgiwrap

nginx配置
smokeping.conf

server {
listen 80;
server_name 192.168.1.158;
#charset koi8-r;
access_log /data/log/nginx/smokeping_access.log ;
error_log /data/log/nginx/smokeping_error.log;
root /opt/smokeping;
index index.html index.htm index.php portal.php default.php;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# Include this file on your nginx.conf to support debian cgi-bin scripts using
# fcgiwrap
location ~ .*\.fcgi$ {
# Disable gzip (it makes scripts feel slower since they have to complete
# before getting gzipped)
gzip off;
# Set the root to /usr/lib (inside this location this means that we are
# giving access to the files under /usr/lib/cgi-bin)
root /opt/smokeping/htdocs;
# Fastcgi socket
fastcgi_pass unix:/var/run/fcgiwrap.socket;
# Fastcgi parameters, include the standard ones
include /etc/nginx/fastcgi_params;
# Adjust non standard parameters (SCRIPT_FILENAME)
#fastcgi_param SCRIPT_FILENAME /usr/lib$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /opt/smokeping/htdocs$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
location ~ /\.ht {
deny all;
}
}

添加测试的cgi页面
helloworld.cgi

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, world.";

smokeping安装

./configure --prefix=/opt/smokeping
make -j4
make install
cd /opt/smokeping
mkdir cache data var
然后修改etc/config文件
chmod 400 /opt/smokeping/etc/smokeping_secrets.dist

smokeping启动

./bin/smokeping --config=/opt/smokeping/etc/config #启动
./bin/smokeping --config=/opt/smokeping/etc/config --debug
./bin/smokeping --config=/opt/smokeping/etc/config --logfile=smoke.log

中文显示

aptitude install fonts-wqy-zenhei

配置etc/config文件

*** Presentation ***
charset = utf-8 #中文显示
*** Targets ***
probe = FPing
menu = Top
title = Network Latency Grapher
remark = Welcome to the SmokePing website of xxx Company. \
Here you will learn all about the latency of our network.
+ Network
menu= Targets
title= network status
#parents = owner:/Test/James location:/
++Lyln
menu = alldevices
title = network status
host = /Network/status1 /Network/status2
++status1
menu = status1
title = status1
host = www.baidu.com
++status2
menu = status2
title = status2
host = www.163.com

参考地址
配置文件

notes:
smokeping踩坑记录:
有图没数据处纠结很久,原因是因为配置的ip地址禁ping了。

Zabbix 3.0 安装小记

zabbix_server部署

1、Zabbix 3.0 for Ubuntu 14.04 LTS:

wget http://repo.zabbix.com/zabbix/3.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.0-1+trusty_all.deb
dpkg -i zabbix-release_3.0-1+trusty_all.deb
apt-get update
apt-get install zabbix-server-mysql zabbix-frontend-php #安装server和frontend
apt-get install zabbix-agent #安装agent

2、初始化数据库

cd /usr/share/doc/zabbix-server-mysql
zcat create.sql.gz | mysql -uroot zabbix

3、修改nginx

server{
server_name zabbix.xxx.com;
listen 80;
root /usr/share/zabbix;
index index.html index.htm index.php portal.php default.php;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:2088;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
#location ~ /\.ht {
#deny all;
#}
}

4、修改php参数
/etc/php5/fpm/php.ini

max_execution_time 300
memory_limit 128M
post_max_size 16M
upload_max_filesize 2M
max_input_time 300
always_populate_raw_post_data -1
date.timezone = UTC

5、zabbix 配置

ListenPort=10051
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=pass
DBSocket=/tmp/mysql.sock
DBPort=3306
JavaGateway=10.254.140.104
JavaGatewayPort=10052
StartJavaPollers=5
ListenIP=10.254.140.104
Timeout=4
AlertScriptsPath=/etc/zabbix/sendEmail
ExternalScripts=/usr/lib/zabbix/externalscripts
FpingLocation=/usr/bin/fping
Fping6Location=/usr/bin/fping6
LogSlowQueries=3000

zabbix_server邮件报警

1、修改zabbix_server配置
zabbix_server.conf
AlertScriptsPath=/etc/zabbix/sendEmail
2、安装sendemail

git clone https://github.com/mogaal/sendemail.git
cp -a sendEmail-v1.XX/sendEmail /usr/local/bin
chmod +x /usr/local/bin/sendEmail

3、添加脚本发邮件脚本
mail.sh

#!/bin/sh
#export.UTF-8 #解决发送邮件中文变成乱码的问题
to=$1
subject=$2
body=$3
/usr/local/bin/sendEmail -f itsupport@sevenga.com -t "$to" -s smtp.exmail.qq.com \
-u "$subject" -o message-content-type=html -o message-charset=utf8 \
-xu user@qq.com -xp mail_pass -m "$body" 2>>/tmp/sendEmail.log

4、web添加媒介类型
Administration->Media types->Create medis type

zabbix-agent安装

修改zabbix_agentd.conf

PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix-agent/zabbix_agentd.log
LogFileSize=0
Server=10.254.140.104 #zabbix server ip
ServerActive=10.254.140.104:10051 #zabbix server ip+port
Hostname=10.136.11.159 # 本机ip
Include=/etc/zabbix/zabbix_agentd.conf.d/

zabbix 监听tomcat

apt-get install zabbix-java-gateway
修改zabbix_java_gateway.conf

LISTEN_IP="10.254.140.104"
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java_gateway.pid"
START_POLLERS=5
TIMEOUT=3

在zabbix_server去掉如下选项注释

JavaGateway=10.254.140.104
JavaGatewayPort=10052
StartJavaPollers=5

zabbix_agent端tomcat设置
1、下载与之对应的
tomcat-catalina-jmx-remote-8.0.36.jar到tomcat的lib目录下
2、修改catalina.sh

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=x.x.x.x"

3、重启tomcat和zabbix_java_gateway、zabbix_server
4、使用cmdline测试

java -jar cmdline-jmxclient-0.10.3.jar - 10.136.11.159:10053 java.lang:type=Memory NonHeapMemoryUsage
08/16/2016 16:20:07 +0800 org.archive.jmx.Client NonHeapMemoryUsage:
committed: 26083328
init: 2555904
max: -1
used: 25317240

5、下载jmx template
jmx template 模板

zabbix_agent远程拉起tomcat

1、zabbix_agentd.conf开启

EnableRemoteCommands=1

2、修改sudoer使zabbix用户免密码执行

zabbix ALL=NOPASSWD: /bin/sh /opt/apache-tomcat-8.0.36/bin/startup.sh
所有命令免密码
zabbix ALL=NOPASSWD: ALL

3、tomcat startup.sh脚本头添加

JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH

1、Zabbix报告系统缺少交换分区空间(“Lack of free swap space”)
由于云主机交换分区没有设置,free -m Swap三项为0。修改触发条件解决。
Configuration->Templates->Template OS Linux->Triggers->Lack of…
修改
{Template OS Linux:system.swap.size[,pfree].last(0)}<50

{Template OS Linux:system.swap.size[,pfree].last(0)}<50
and {Template OS Linux:system.swap.size[,free].last(0)}<>0

判断系统有交换空间,当系统无交换空间值为0时,不触发。

2、zabbix图形中文乱码
win下找到SIMHEI.TTF字体,上传至/usr/local/zabbix/fonts
然后修改/usr/local/zabbix/include/defines.inc.php 文件

define('ZBX_GRAPH_FONT_NAME', 'simhei'); // font file name

links:
https://www.zabbix.com/documentation/3.0/manual/installation/install_from_packages#debianubuntu

Supervisor进程管理

Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程。

Supervisor安装

apt-get install supervisor

Supervisor配置文件

默认配置文件/etc/supervisor/supervisord.conf
如过没有,生成默认配置文件

echo_supervisord_conf > /etc/supervisor/supervisord.conf

[supervisord] #supervisord本身的日志配置
logfile=/var/log/supervisor/supervisord.log ;
pidfile=/var/run/supervisord.pid ;

添加管理的java进程
hello.conf

[program:showchat]
directory = /home/ljd/hello
command = /usr/local/jdk/bin/java -Xmx512M -Xms512M -classpath hello.jar com.ljd.Hello
autostart = true #子进程随supervisord启动而启动
startsecs = 5 #进程启动后跑了几秒,才被认定为成功启动。默认1
autorestart = true #子进程挂掉将被无条件重启
numprocs=1 #启动线程数目
user = rgbvr #指定运行用户
environment=CHAT_SETTING_HOME=/opt/hello/config #指定程序环境变量
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
redirect_stderr = true
stdout_logfile = /home/ljd/hello.log

#