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使用

    原文作者:徐浩友
    原文地址: https://www.jianshu.com/p/5c6ec8e10fb1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞