CentOS6 基於 Python 安裝 Sentry

jormin發表於2017-03-30

概述

Sentry 是一款可用於Production環境的錯誤跟蹤工具,可實時通知Production環境中使用者由於一些不可預料行為(或者程式寫的有問題)導致程式Crash或Exception,Sentry可以通過Integration如HipChat來傳送通知,並且可以通過JIRA Integration來快速建立Issue,然後開發者可以根據這個Issue快速修復程式,並把這個已修復的Hotfix快速部署到生產環境,這樣就快速開發快速修補。

Sentry 提供了兩種使用方式:

  1. 廠商的服務,提供14天無限制免費服務,點選檢視 官網價格服務使用簡介
  2. 原始碼 自行搭建,包括 DockerPython 兩種方式, Python 安裝方式Here Docker 安裝方式Here

安裝依賴軟體

安裝Sentry需要以下滿足以下條件:

  • 基於UNIX的作業系統
  • PostgreSQL:版本 9.5 以上,推薦最新版本 9.6
  • Redis:最低版本 2.8.9, 推薦 2.8.18, 3.0
  • Python 2.7 :點選檢視安裝方式
  • Pip 8.1+
  • 安裝下列軟體:python-setuptools, python-dev, libxslt1-dev, gcc, libffi-dev, libjpeg-dev, libxml2-dev, libxslt-dev, libyaml-dev, libpq-dev,推薦 yum 安裝。

實際安裝過程中,最好預留1G的記憶體,我安裝時因為預留記憶體不夠折騰了好久,最後翻 githubissue 才意識到是記憶體的問題。

安裝PostgreSQL

安裝新版的yum源

我使用的是官方當前最新版本 9.6,其他版本詳見官方文件,根據自己要安裝的版本及作業系統選擇下載 rpm 檔案即可。

$ cd ~
$ wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
$ rpm -ivh ./pgdg-redhat96-9.6-3.noarch.rpm

移除之前的版本

$ rpm -qa | grep postgre
postgresql84-devel-8.4.3-2PGDG.rhel6.x86_64
postgresql84-libs-8.4.3-2PGDG.rhel6.x86_64
postgresql84-contrib-8.4.3-2PGDG.rhel6.x86_64
postgresql84-server-8.4.3-2PGDG.rhel6.x86_64
postgresql84-8.4.3-2PGDG.rhel6.x86_64

$ yum remove postgresql84 postgresql84-devel postgresql84-libs postgresql84-contrib postgresql84-server

檢視安裝新版 yum 源後可以安裝的版本

$ yum list postgres* | grep 96
postgresql96.x86_64                        9.6.2-2PGDG.rhel6             @pgdg96
postgresql96-contrib.x86_64                9.6.2-2PGDG.rhel6             @pgdg96
postgresql96-devel.x86_64                  9.6.2-2PGDG.rhel6             @pgdg96
postgresql96-libs.x86_64                   9.6.2-2PGDG.rhel6             @pgdg96
postgresql96-server.x86_64                 9.6.2-2PGDG.rhel6             @pgdg96
postgresql-jdbc.noarch                     42.0.0-1.rhel6                pgdg96
postgresql-jdbc-javadoc.noarch             42.0.0-1.rhel6                pgdg96
postgresql-unit96.x86_64                   2.0-1.rhel6                   pgdg96
postgresql-unit96-debuginfo.x86_64         2.0-1.rhel6                   pgdg96
postgresql96-debuginfo.x86_64              9.6.2-2PGDG.rhel6             pgdg96
postgresql96-docs.x86_64                   9.6.2-2PGDG.rhel6             pgdg96
postgresql96-odbc.x86_64                   09.06.0100-1PGDG.rhel6        pgdg96
postgresql96-plperl.x86_64                 9.6.2-2PGDG.rhel6             pgdg96
postgresql96-plpython.x86_64               9.6.2-2PGDG.rhel6             pgdg96
postgresql96-pltcl.x86_64                  9.6.2-2PGDG.rhel6             pgdg96
postgresql96-tcl.x86_64                    2.1.1-1.rhel6                 pgdg96
postgresql96-tcl-debuginfo.x86_64          2.1.1-1.rhel6                 pgdg96
postgresql96-test.x86_64                   9.6.2-2PGDG.rhel6             pgdg96

從結果可以看出新版 yum 源可以安裝很多依賴,但實際上我們只需要安裝 postgresql96postgresql96-develpostgresql96-libspostgresql96-server 四項即可。

$ yum install postgresql96 postgresql96-devel postgresql96-libs postgresql96-server
$ # 初始化資料庫
$ /etc/init.d/postgresql-9.6 initdb
$ # 開啟資料庫
$ /etc/init.d/postgresql-9.6 start

至此,新版 PostgreSQL 就安裝好了,接下來是做些連線配置:

$ vim /var/lib/pgsql/9.6/data/pg_hba.conf

將相關連線的 Method 改為 trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local           all              all                                            trust
# IPv4 local connections:
host            all                 all             127.0.0.1/32            trust
# IPv6 local connections:
host            all                 all             ::1/128                     trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

安裝Redis

安裝redis相對簡單,使用 yum 安裝即可

$ yum install redis
$ # 開啟redis
$ /etc/init.d/redis start

安裝Sentry

安裝軟體

$ # 安裝環境
$ pip install -U virtualenv
$ virtualenv /www/sentry/
$ source /www/sentry/bin/activate
$ # 安裝sentry
$ pip install -U sentry

相關配置

初始化配置,如果不設定路徑的話,預設會生成在 ~/.sentry 目錄中,推薦使用預設路徑。初始化配置包含兩個檔案,分別是 config.ymlsentry.conf.py

$ sentry init

郵件配置項儲存在 config.yml 檔案中,Sentry 收到異常時會給自己的郵箱傳送郵件通知,所以需要配置下郵件,當然也可以不啟用,但不建議這樣做。

# 如果不想啟用郵件通知,可以將 mail.backend 設定為 dummy 
mail.backend: 'smtp'
mail.host: 'localhost'
mail.port: 25
mail.username: ''
mail.password: ''
mail.use-tls: false
mail.from: 'sentry@localhost'

資料庫配置項儲存在 sentry.conf.py 檔案中,使用預設即可,只需要修改 HOSTPORT 項。

# ~/.sentry/sentry.conf.py

# for more information on DATABASES, see the Django configuration at:
# https://docs.djangoproject.com/en/1.6/ref/databases/
DATABASES = {
    'default': {
        'ENGINE': 'sentry.db.postgres',
        'NAME': 'sentry',
        'USER': 'postgres',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Redis 項使用預設即可,另外一個需要注意的是 Web 使用的埠配置項 SENTRY_WEB_PORT,預設為 9000 ,實際安裝時可以檢測下埠是否已被佔用,我安裝時發現被佔用,所以改用了 9999

使用資料遷移初始化資料

建立資料庫

$ createdb -U postgres -E utf-8 sentry

執行資料更新,資料填充完成後會提示是否建立使用者,可以選擇不建立後續再建立,另外,這一步比較 耗記憶體,我就是在執行這一步的時候出了很多意外,比如程式被 killed,報很多異常等等。

$ sentry upgrade
$ # 如果沒有使用預設目錄的話,需要使用如下命令
$ # SENTRY_CONF=/path/to/sentry sentry upgrade

建立使用者,需要填寫郵箱和密碼,建立好後會提示是否作為超級使用者,根據自己情況選擇即可。

$ sentry createuser
$ # 如果沒有使用預設目錄的話,需要使用如下命令
$ # SENTRY_CONF=/path/to/sentry sentry createuser

開啟服務

開啟 web 服務,開啟後可以訪問 http://[IP或域名]:[前面配置的埠] 進行訪問。

$ sentry run web
$ # 如果沒有使用預設目錄的話,需要使用如下命令
$ # SENTRY_CONF=/path/to/sentry sentry run web

開啟後臺 worker 服務

$ sentry run worker
$ # 如果沒有使用預設目錄的話,需要使用如下命令
$ # SENTRY_CONF=/path/to/sentry sentry run worker

開啟 cron 程式

$ sentry run cron
$ # 如果沒有使用預設目錄的話,需要使用如下命令
$ # SENTRY_CONF=/path/to/sentry sentry run cron

Laravel整合擴充套件包

Sentry官方針對各種語言的不同框架都提供了相應的擴充套件包,當然也包括 laravel

配置好後就可以在自己的專案中愉快的使用了,悟禪小書童 已經使用上了,參考圖如下:

相關文章