sonarqube && sonar-scanner

sonarqube && sonar-scanner

link
sonarqube && sonar-scanner

sonarqube

download
archive
github-sonarqube
sonarqube-7.9.3-lts

sonarqube-lts-7.9 requirement

link

opt-env

  • openjdk-11
  • postgresql-10
  • centos-7.x 2c4g

openjdk-11

openjdk
download

01、mkdir home && unzip

[ ~]# mkdir /opt/sonarqube  //项目运行目录
[ ~]# tar zxf openjdk-11.0.2_linux-x64_bin.tar.gz  -C /opt/sonarqube/
[ ~]# 
[ ~]# cd /opt/sonarqube/
[ sonarqube]# ll
total 0
drwxr-xr-x 8 root root 96 Jun  1 23:47 jdk-11.0.2
[ sonarqube]# cd jdk-11.0.2/
[ jdk-11.0.2]# pwd
/opt/sonarqube/jdk-11.0.2

02、add openjdk-env

cat <<-‘EOF‘ >>/etc/profile
#add openjdk-11
export JAVA_HOME=/opt/sonarqube/jdk-11.0.2
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
EOF

[ jdk-11.0.2]# source /etc/profile
[ jdk-11.0.2]# 
[ jdk-11.0.2]# java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
[ jdk-11.0.2]#

postgresql-10

download
reference

01、requirement

[ postgresql-10.10]# yum install -y gcc gcc-c++ make autoconf
[ jdk-11.0.2]# yum install -y ncurses-devel readline-devel zlib-devel

02、adduser && pgdata && pg_env

[ ~]# useradd postgres
[ ~]# mkdir -p /opt/sonarqube/pgdata/{data,archive}
su - postgres   //配置环境变量
tee <<-‘EOF‘ >>.bash_profile
export PGHOME=/opt/sonarqube/pgdata
export PGDATA=$PGHOME/data
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=.:$PGHOME/lib
EOF
source .bash_profile

03、compile && init

[ ~]# yum install -y lbzip2
[ ~]# tar zxf postgresql-10.10.tar.bz2
[ postgresql-10.10]# ./configure   -q --prefix=/opt/sonarqube/pgdata
[ postgresql-10.10]# make -s -j4
[ postgresql-10.10]# make -s install
[ postgresql-10.10]# chown -R postgres: /opt/sonarqube/pgdata/
su - postgres
initdb --auth=trust --auth-host=md5 --auth-local=trust --pgdata=$PGDATA --encoding=‘UTF-8‘ --username=postgres --pwprompt
[ data]$ pwd
/opt/sonarqube/pgdata/data
[ data]$ 
tee <<-‘EOF‘ >> postgresql.auto.conf
listen_addresses = ‘*‘
port = 5432
wal_level = hot_standby
archive_mode = on
archive_command = ‘cp %p /opt/sonarqube/pgdata/archive/%f‘
#max_wal_senders = 10
logging_collector = on
EOF

[ data]$ egrep -v ‘^#|^$‘ pg_hba.conf 
local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5
[ ~]$ pg_ctl start  //start pg
waiting for server to start....2020-06-02 00:14:57.195 CST [20182] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-06-02 00:14:57.195 CST [20182] LOG:  listening on IPv6 address "::", port 5432
2020-06-02 00:14:57.196 CST [20182] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-06-02 00:14:57.211 CST [20182] LOG:  redirecting log output to logging collector process
2020-06-02 00:14:57.211 CST [20182] HINT:  Future log output will appear in directory "log".
 done
server started
[ ~]$ 
[ ~]$ ss -lnt
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port              
LISTEN      0      128                                    *:22                                                 *:*                  
LISTEN      0      128                                    *:5432                                               *:*                  
LISTEN      0      100                            127.0.0.1:25                                                 *:*                  
LISTEN      0      128                                   :::22                                                :::*                  
LISTEN      0      128                                   :::5432                                              :::*                  
LISTEN      0      100                                  ::1:25                                                :::*                  
[ ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \q
[ ~]$

04、init sonar db

su - postgres
psql

create database sonar;
create user sonar with superuser createdb createrole login password ‘123123‘;
alter database sonar owner to sonar;

sonarqube-lts-7.9

01、linux-sonar turn
link

tee <<-EOF  >>/etc/sysctl.conf
#add sonarqube
vm.max_map_count=262144
fs.file-max=102400
EOF

sysctl -p

tee <<-EOF  >>/etc/security/limits.conf
#add sonarqube
sonar  -  nofile 102400
sonar  -  nproc  102400
EOF

02、add user && config

[ ~]# useradd sonar
[ ~]# yum install -y unzip
[ ~]# unzip sonarqube-7.9.3.zip  -d /opt/sonarqube/
[ ~]# chown -R sonar: /opt/sonarqube/sonarqube-7.9.3/

[ conf]# pwd
/opt/sonarqube/sonarqube-7.9.3/conf

[03 conf]# egrep -v ‘^#|^$‘ sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=123123
sonar.jdbc.url=jdbc:postgresql://192.168.3.103/sonar
sonar.web.javaOpts=-Xmx1024m -Xms1024m -XX:+HeapDumpOnOutOfMemoryError
sonar.web.host=0.0.0.0
[ conf]#

03、privileges && start

[ conf]# su - sonar   //change sonar to running
[ ~]$ /opt/sonarqube/sonarqube-7.9.3/bin/linux-x86-64/sonar.sh start |status |stop

[ linux-x86-64]$ ./sonar.sh --help
Usage: ./sonar.sh { console | start | stop | force-stop | restart | status | dump }

[ ~]$ tail -F /opt/sonarqube/sonarqube-7.9.3/logs/sonar.log 

[ logs]$ pwd
/opt/sonarqube/sonarqube-7.9.3/logs
[ logs]$ ll
total 212
-rw-r--r-- 1 sonar sonar  54757 Jun  2 11:28 access.log
-rw-r--r-- 1 sonar sonar   1136 Jun  2 11:26 ce.log
-rw-r--r-- 1 sonar sonar  11500 Jun  2 11:26 es.log
-rw-r--r-- 1 sonar sonar     88 Mar 24 08:24 README.txt
-rw-r--r-- 1 sonar sonar   4978 Jun  2 11:26 sonar.log
-rw-r--r-- 1 sonar sonar 128911 Jun  2 11:26 web.log
[ logs]$

04、open sonarqube web

http://192.168.3.103:9000/
admin/admin    //default user

sonar-scanner

link
doc

01、download && unzip

[ ~]# unzip sonar-scanner-cli-4.3.0.2102-linux.zip  -d /opt/sonarqube/
[ ~]# chown -R sonar: /opt/sonarqube/sonar-scanner-4.3.0.2102-linux/

[ sonar-scanner-4.3.0.2102-linux]$ ll bin/
total 8
-rwxr-xr-x 1 sonar sonar 1761 Feb 20 08:14 sonar-scanner
-rwxr-xr-x 1 sonar sonar  595 Feb 20 08:14 sonar-scanner-debug
[ sonar-scanner-4.3.0.2102-linux]$ ./bin/sonar-scanner -h
INFO: 
INFO: usage: sonar-scanner [options]
INFO: 
INFO: Options:
INFO:  -D,--define <arg>     Define property
INFO:  -h,--help             Display help information
INFO:  -v,--version          Display version information
INFO:  -X,--debug            Produce execution debug output
[ sonar-scanner-4.3.0.2102-linux]$ 
[ sonar-scanner-4.3.0.2102-linux]$ 
[ sonar-scanner-4.3.0.2102-linux]$ 
[ sonar-scanner-4.3.0.2102-linux]$ ./bin/sonar-scanner -v
INFO: Scanner configuration file: /opt/sonarqube/sonar-scanner-4.3.0.2102-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.3.0.2102
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 3.10.0-862.el7.x86_64 amd64
[ sonar-scanner-4.3.0.2102-linux]$

02、add env && config

tee <<-‘EOF‘ >> ~/.bash_profile
#add sonaqube env
export SONAR_HOME=/opt/sonarqube/sonarqube-7.9.3
export SONAR_SCANNER_HOME=/opt/sonarqube/sonar-scanner-4.3.0.2102-linux
export PATH=$SONAR_SCANNER_HOME/bin:$PATH
export SONAR_SCANNER_OPTS="-Xmx1024m"
EOF

source ~/.bash_profile
[ conf]$ cat sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://192.168.3.103:9000

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

03、scanner codes

进入到待扫描代码根目录新建sonar-project.properties
#sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=my:project

# --- optional properties ---

# defaults to project key
#sonar.projectName=My project
# defaults to ‘not provided‘
#sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

#path to your project build output path
#sonar.java.binaries=target/classes

sonar-scaneer  //代码根目录执行

sonar.projectName是项目名字
sonar.sources=是源文件所在的目录

Sample Projects

To help you get started, simple project samples are available for most languages on github. They can be browsed or downloaded. You‘ll find them filed under sonarqube-scanner/src.

daily-opt

postgresql

su - postgres
pg_ctl start
ps -ef | grep postgres //查看pgsql进程
pg_ctl status //查看数据状态
pg_ctl stop -m fast //停止数据库

troubleshooting

对于多语言扫描请预先安装对应的编译语言组件 go/node/python ...

reference

link

相关推荐