Centos7部署Sentry實(shí)戰(zhàn)

Sentry 由python開(kāi)發(fā),django為框架的跨平臺(tái)多語(yǔ)言/框架的日志聚合平臺(tái),功能十分強(qiáng)悍。截至目前最新版本是8.22.0

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

下面以Centos7平臺(tái)下分三部處理sentry:

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

一.安裝

首先安裝sentry所依賴的PostgreSQL數(shù)據(jù)庫(kù)。

# 在系統(tǒng)中添加官網(wǎng)提供的對(duì)應(yīng)版本的Yum倉(cāng)庫(kù), 如下是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數(shù)據(jù)庫(kù)
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

接下來(lái)配置PostgreSQL依賴的環(huán)境,然后啟動(dòng)數(shù)據(jù)庫(kù)

sudo mkdir -p /data/pgsql                   # 數(shù)據(jù)庫(kù)物理文件的存放目錄
sudo chown postgres:postgres /data/pgsql    # 所屬組/成員
sudo su - postgres                          
cp /etc/skel/.bash* /var/lib/pgsql

編輯 /var/lib/pgsql/.bashrc文件,設(shè)置PGDATE環(huán)境變量并添加pgsql的bin路徑到PATH變量。

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

使環(huán)境生效:

source .bashrc

然后初始化數(shù)據(jù)庫(kù)并啟動(dòng):

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

$ [postgres@me ~]$ pg_ctl start

查看 tail /data/pgsql/pg_log/postgresql-*.log 日志信息:
接下來(lái)為Sentry提供PostgreSQL數(shù)據(jù)庫(kù)信息:

[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

這些依賴安裝和配置成功后就開(kāi)始安裝sentry:

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

結(jié)果報(bào)錯(cuò)了, pg_config executable not found, 在這里pg_config executable not found有解決方案,原因就是pg_config可執(zhí)行文件不在PATH環(huán)境變量里, pg_config就是上面安裝PostgreSQL的文件。

$ which pg_config
... 沒(méi)有

.bashrc下添加PATH:

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

然后激活環(huán)境: source ~/.bashrc
接下來(lái)安裝:

pip install psycopg2 sentry

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

安裝并啟動(dòng)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數(shù)據(jù)庫(kù)初始化,然后將sentry的用戶權(quán)限改為普通

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

生效并啟動(dòng)配置:

supervisord -c /etc/supervisor.conf   #啟動(dòng)supervisord
supervisorctl -c /etc/supervisor.conf reread    # 讀取配置
supervisorctl -c /etc/supervisor.conf update    # 啟動(dòng)
supervisorctl -c /etc/supervisor.conf status    # 查看
supervisorctl -c /etc/supervisor.conf restart all  #重啟所有任務(wù)

這里總結(jié)下supervisord常見(jiàn)的問(wèn)題和解決方案:

1. 當(dāng) supervisord 啟動(dòng)異常時(shí),可 `supervisord -n` 前臺(tái)啟動(dòng)查看詳細(xì)信息
2. 一般supervisor異常分兩種,配置不對(duì)或權(quán)限的問(wèn)題。
3. 善用日志信息,首先要在supervisord.conf設(shè)置的日志下查看詳情。

配置計(jì)劃任務(wù),清除歷史數(shù)據(jù)

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

sentry的使用參考:sentry使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容