Centos7部署Sentry实战

Sentry 由python开发,django为框架的跨平台多语言/框架的日志聚合平台,功能十分强悍。截至目前最新版本是8.22.0

$ python -c "import sentry; print(sentry.__version__)"
8.22.0

下面以Centos7平台下分三部处理sentry:

  1. 安装
  2. 配置
  3. 使用

一.安装

首先安装sentry所依赖的PostgreSQL数据库。

# 在系统中添加官网提供的对应版本的Yum仓库, 如下是9.6版本
sudo yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm

# 然后安装PostgreSQL数据库
yum update      # 这一步可省略
yum -y install vim python-setuptools python-dev libxslt1-dev gcc libffi-dev libjpeg-dev libxml2-dev libxslt-dev libyaml-dev libpq-dev
yum install postgresql96-server postgresql96-contrib postgresql-devel

接下来配置PostgreSQL依赖的环境,然后启动数据库

sudo mkdir -p /data/pgsql                   # 数据库物理文件的存放目录
sudo chown postgres:postgres /data/pgsql    # 所属组/成员
sudo su - postgres                          
cp /etc/skel/.bash* /var/lib/pgsql

编辑 /var/lib/pgsql/.bashrc文件,设置PGDATE环境变量并添加pgsql的bin路径到PATH变量。

export PGDATA=/data/pgsql
export PATH=/usr/pgsql-9.6/bin:$PATH

使环境生效:

source .bashrc

然后初始化数据库并启动:

$ [postgres@me ~]$ initdb  # 初始化
$ [postgres@me ~]$ ls /data/pgsql/
..

$ [postgres@me ~]$ pg_ctl start

查看 tail /data/pgsql/pg_log/postgresql-*.log 日志信息:
接下来为Sentry提供PostgreSQL数据库信息:

[postgres@me ~]$ psql
psql (9.6.1)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres=# CREATE USER sentry WITH PASSWORD '*****';
CREATE ROLE
postgres=# CREATE DATABASE sentry OWNER sentry;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE sentry to sentry;
GRANT
ALTER USER sentry WITH SUPERUSER;
ALTER ROLE
postgres=# \q

这些依赖安装和配置成功后就开始安装sentry:

$ sudo pip install sentry
...
Error: pg_config executable not found

结果报错了, pg_config executable not found, 在这里pg_config executable not found有解决方案,原因就是pg_config可执行文件不在PATH环境变量里, pg_config就是上面安装PostgreSQL的文件。

$ which pg_config
... 没有

.bashrc下添加PATH:

export PATH=$PATH:/usr/pgsql-9.6/bin/

然后激活环境: source ~/.bashrc
接下来安装:

pip install psycopg2 sentry

# 查看是否安装成功
python -c "import sentry;print(sentry.__version__)"

安装并启动redis

yum -y install redis
/usr/bin/redis-server /etc/redis.conf

二. 配置

首先初始化sentry生成配置文件:

[root@me ~]sentry init /etc/sentry
[root@me ~]cd /etc/sentry
[root@me /etc/sentry]$ tree
.
├── config.yml
└── sentry.conf.py

其中在sentry.conf.py下配置 PostgreSQL DATABASES 和 BROKER_URL:

DATABASES = {
    'default': {
        'ENGINE': 'sentry.db.postgres',
        'NAME': 'sentry',
        'USER': 'app',
        'PASSWORD': '*****',
        'HOST': 'localhost',
        'PORT': '5432',
        'AUTOCOMMIT': True,
        'ATOMIC_REQUESTS': False,
    }
}

BROKER_URL = "redis://:password@localhost:6379/2"

config.yaml 文件配置如下:

# While a lot of configuration in Sentry can be changed via the UI, for all
# new-style config (as of 8.0) you can also declare values here in this file
# to enforce defaults or to ensure they cannot be changed via the UI. For more
# information see the Sentry documentation.

###############
# Mail Server #
###############

mail.backend: 'smtp'  # Use dummy if you want to disable email entirely
mail.host: 'smtp.263.net'
mail.port: 465
mail.username: 'support@xxx.cn'
mail.password: '****'
mail.use-tls: true
# The email address to send on behalf of
mail.from: 'support@***.cn'

# If you'd like to configure email replies, enable this.
# mail.enable-replies: false

# When email-replies are enabled, this value is used in the Reply-To header
# mail.reply-hostname: ''

# If you're using mailgun for inbound mail, set your API key and configure a
# route to forward to /api/hooks/mailgun/inbound/
# mail.mailgun-api-key: ''

###################
# System Settings #
###################

# If this file ever becomes compromised, it's important to regenerate your a new key
# Changing this value will result in all current sessions being invalidated.
# A new key can be generated with `$ sentry config generate-secret-key`
system.secret-key: '(80z$b)5u)oxq^kkzr^1r5%6^_4y3hrv0+!nlp!q1w+^fb#yux'

# The ``redis.clusters`` setting is used, unsurprisingly, to configure Redis
# clusters. These clusters can be then referred to by name when configuring
# backends such as the cache, digests, or TSDB backend.
redis.clusters:
  default:
    hosts:
      0:
        host: 127.0.0.1
        port: 6379
        password: '*****'

这里注意,如果邮件使用SSL, 则需要 SMTP SSL email backend for Django

pip install django-smtp-ssl

sentry数据库初始化,然后将sentry的用户权限改为普通

SENTRY_CONF=/etc/sentry sentry upgrade

psql
ALTER USER sentry WITH NOSUPERUSER;
ALTER ROLE
postgres=# \q

然后配置 supervisor文件:

[root@me ~]# vim /etc/supervisord.d/sentry.ini
[program:sentry-web]
environment=SENTRY_CONF="/etc/sentry"
command=sentry run web
autostart=true
autorestart=true
user=sentry
stdout_logfile=/home/sentry/logs/web.log
stderr_logfile=/home/sentry/logs/web.log

[program:sentry-worker]
environment=SENTRY_CONF="/etc/sentry"
command=sentry run worker
autostart=true
autorestart=true
user=sentry
stdout_logfile=/home/sentry/logs/worker.log
stderr_logfile=/home/sentry/logs/worker.log

[program:sentry-cron]
environment=SENTRY_CONF="/etc/sentry"
command=sentry run cron
autostart=true
autorestart=true
user=sentry
stdout_logfile=/home/sentry/logs/cron.log
stderr_logfile=/home/sentry/logs/cron.log

生效并启动配置:

supervisord -c /etc/supervisor.conf   #启动supervisord
supervisorctl -c /etc/supervisor.conf reread    # 读取配置
supervisorctl -c /etc/supervisor.conf update    # 启动
supervisorctl -c /etc/supervisor.conf status    # 查看
supervisorctl -c /etc/supervisor.conf restart all  #重启所有任务

这里总结下supervisord常见的问题和解决方案:

1. 当 supervisord 启动异常时,可 `supervisord -n` 前台启动查看详细信息
2. 一般supervisor异常分两种,配置不对或权限的问题。
3. 善用日志信息,首先要在supervisord.conf设置的日志下查看详情。

配置计划任务,清除历史数据

$ crontab -e
0 3 * * * sentry cleanup --days=30

sentry的使用参考:sentry使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容