sonaqube是一個開源代碼質量管理平臺,致力於持續分析和測量技術質量。java
SonarQube平臺由4部分組成:python
系統構成和相互關係:mysql
sonarqube安裝很簡單,下載(直接win下載,而後上傳到linux服務器)安裝包後直接解壓便可:linux
# 建立sonar用戶及工做目錄 $ useradd sonar # 解壓 $ unzip sonarqube-6.7.5.zip $ cd /home/sonar/sonarqube-6.7.5 # 在當前終端啓動(方便排錯) $ ./bin/linux-x86-64/sonar.sh console # 直接在後臺運行 $ ./bin/linux-x86-64/sonar.sh start
說明: sonarqube依賴於Elasticsearch插件,es插件不能用root運行,因此使用普通用戶運行sonar,不然將會出現以下報錯:web
Caused by: java.lang.RuntimeException: can not run elasticsearch as root # Elasticsearch不能用root運行。
參考:https://blog.csdn.net/zdyueguanyun/article/details/79447260sql
$ netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 771/sshd tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 5258/java tcp6 0 0 :::22 :::* LISTEN 771/sshd tcp6 0 0 127.0.0.1:9092 :::* LISTEN 5343/java tcp6 0 0 127.0.0.1:35048 :::* LISTEN 5443/java tcp6 0 0 :::9000 :::* LISTEN 5343/java tcp6 0 0 127.0.0.1:9001 :::* LISTEN 5273/java
默認由9000端口提供UI管理頁面,在瀏覽器訪問:http://192.168.228.129:9000 ,界面以下(管理員:admin,密碼:admin): 數據庫
建立一個單獨的庫sonar並受權給sonar用戶:vim
mysql> create database sonar; mysql> CREATE USER 'sonarqube'@'%' IDENTIFIED BY '123456';
mysql調優:瀏覽器
$ vim /etc/my.cnf binlog-format=MIXED # 指定binlog格式爲mixed。默認爲STATEMENT,數據沒法正常migration innodb_buffer_pool_size = 200M # 最佳數值爲70%~80%服務器內存 query_cache_size = 15M # 參考:https://www.percona.com/blog/2007/11/01/innodb-performance-optimization-basics/ $ systemctl restart mysqld
排錯1:bash
沒法初始化數據庫:
# 報錯:logs/web.log Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging. InnoDB is limited to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.
解決辦法:
$ vim /etc/my.cnf binlog-format=MIXED # 指定binlog格式爲mixed。默認爲STATEMENT,數據沒法正常 # 參考:https://www.devside.net/wamp-server/mysql-error-impossible-to-write-to-binary-log-since-binlog_format-statement
排錯2:
歷史髒數據衝突:
# 報錯: Web server startup failed: Current version is too old. Please upgrade to Long Term Support version firstly.
解決辦法: 清理歷史數據(因首次安裝,直接drop掉sonar表,而後重建),而後重啓sonar便可!
sysctl -w vm.max_map_count=262144 sysctl -w fs.file-max=65536 ulimit -n 65536 ulimit -u 2048
默認狀況下,Elasticsearch數據存儲在 <install_directory> / data中,但不建議用於生產實例。相反,您應該將此數據存儲在其餘位置,最好是在具備快速I / O的專用卷中。除了保持可接受的性能以外,這樣作還能夠簡化SonarQube的升級。
$ mkdir -p /var/sonarqube/data $ mkdir -p /var/sonarqube/temp $ chown -R sonar:sonar /var/sonarqube
$ vim conf/sonar.properties sonar.jdbc.username=sonar # 數據庫用戶名 sonar.jdbc.password=123456 # 數據庫密碼 sonar.jdbc.url=jdbc:mysql://192.168.228.129:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false # 數據庫服務器 sonar.path.data=/var/sonarqube/data sonar.path.temp=/var/sonarqube/temp $ ./bin/linux-x86-64/sonar.sh restart
$ vim /usr/lib/systemd/system/sonar.service [Unit] Description=SonarQube service After=network.target syslog.target [Service] Type=forking User=sonar Group=sonar ExecStart=/bin/bash /home/sonar/sonarqube-6.7.5/bin/linux-x86-64/sonar.sh start ExecStop=/bin/bash /home/sonar/sonarqube-6.7.5/bin/linux-x86-64/sonar.sh stop Restart=always LimitNOFILE=65536 [Install] WantedBy=multi-user.target $ systemctl enable sonar # 開機啓動
部署sonaqube集羣: https://docs.sonarqube.org/display/SONAR/Installing+a+SonarQube+cluster
管理插件的方法有以下兩種:
*$SONARQUBE_HOME/extensions/plugins
,刪除舊版本的插件;systemctl restart sonar
。直接使用admin用戶在UI界面安裝(可能有點慢):
安裝完成後「Restart」生效,能夠再sonar服務器查看:
若是因網絡問題安裝失敗,能夠手動安裝:
參考:https://docs.sonarqube.org/display/SONAR/LDAP+Integration#LDAPIntegration-ConfigurationExamples
示例:
# 配置ldap服務器 sonar.security.realm=LDAP ldap.url=ldap://ldap.chunyu.club ldap.bindDn=cn=reader,dc=chunyu,dc=club ldap.bindPassword=xxxxxxxxxxx # User Configuration ldap.user.baseDn=OU=People,DC=chunyu,DC=club ldap.user.request=(uid={login}) ldap.user.realNameAttribute=uid ldap.user.emailAttribute=mail # Group Configuration ldap.group.baseDn=ou=groups,DC=chunyu,DC=club ldap.group.request=(&(objectClass=posixGroup)(memberUid={uid}))
官方文檔:https://docs.sonarqube.org/display/SONAR/Upgrading
下載地址 ,本地下載而後上傳到sonar服務器。
# 解壓到任何目錄均可以,後續經過系統環境變量配置scanner相關命令 $ unzip sonar-scanner-cli-3.2.0.1227-linux.zip ## 爲了方便管理,將解壓文件放到sonar安裝目錄下:/home/sonar/sonarqube-6.7.5/ $ mv sonar-scanner-3.2.0.1227-linux /home/sonar/sonarqube-6.7.5/sonar-scanner-3.2.0 $ chown -R sonar:sonar /home/sonar/sonarqube-6.7.5/
$ vim /etc/profile export SONAR_SCANNER_HOME=/home/sonar/sonarqube-6.7.5/sonar-scanner-3.2.0 export PATH=${SONAR_SCANNER_HOME}/bin:${PATH} $ source /etc/profile # 檢查配置結果 $ sonar-scanner -v INFO: Scanner configuration file: /home/sonar/sonarqube-6.7.5/sonar-scanner-3.2.0/conf/sonar-scanner.properties INFO: Project root configuration file: NONE INFO: SonarQube Scanner 3.2.0.1227 INFO: Java 1.8.0_121 Oracle Corporation (64-bit) INFO: Linux 3.10.0-693.el7.x86_64 amd64 # Success!
進入要進行代碼分析的項目根目錄,新建sonar-project.properties文件,內容以下:
# must be unique in a given SonarQube instance sonar.projectKey=test:python # this is the name displayed in the SonarQube UI sonar.projectName=chunyu_community # 要展現在UI界面的項目名稱 sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # Since SonarQube 4.2, this property is optional if sonar.modules is set. # If not set, SonarQube starts looking for source code from the directory containing # the sonar-project.properties file. sonar.sources=./ # 項目文件目錄 # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8
執行命令:
$ sonar-scanner
待上述命令執行結束後即可以在UI界面看到掃描結果: