sonarqube 搭建过程

SonarQube简介

Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。比如pmd-cpd、checkstyle、findbugs、Jenkins。通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言(支持的语言包括:Java、PHP、C#、C、Cobol、PL/SQL、Flex等)提供支持,对国际化以及报告文档化也有良好的支持。可以说Sonar是目前最强大的代码质量管理工具之一。
下面就来揭开Sonar的神秘面纱,看看如何在Centos环境上安装Sonar。

安装环境:

系统环境:centos6.9 -i686(最小化安装)
前置条件:jdk1.8 , mysql-5.7
软件下载目录:/usr/local/
软件安装目录:/usr/local/
软件版本:sonarqube-6.7.5,sonar-scanner-cli-3.2.0.1227-linux

先决条件:

vm.max_map_count 大于或等于262144
fs.file-max 大于或等于65536
运行SonarQube的用户可以打开至少65536个文件描述符
运行SonarQube的用户可以打开至少2048个线程

可以在/etc/sysctl.conf和/etc/security/limits.conf文件中设置

配置环境变量
java安装1.8版本,在/etc/profile添加路径。

Mysql安装5.7版本,设置mysql的环境通过文档资料https://blog.csdn.net/Luck_ZZ/article/details/80341693

Sonarqube在/etc/profile添加路径

Sonar-scanner在/etc/profile添加路径

配置sonar.properties和sonar-scanner

配置sonar-scanner的项目配置sonar-project.properties
模板:
sonar.projectKey=my:project
sonar.projectName=yaok
sonar.projectVersion=1.0

#Paths to source directories.

#Paths are relative to the sonar-project.properties file. Replace “\” by “/“ on Windows.

#Do not put the “sonar-project.properties” file in the same directory with the source code.

#(i.e. never set the “sonar.sources” property to “.”)
sonar.sources=/root/java

#The value of the property must be the key of the language.
sonar.language=java

#Encoding of the source code
sonar.sourceEncoding=UTF-8

启动sonar

Root用户无法启动,需要新建用户
[root@localhost ]# useradd sonaruser
[root@localhost local]# chown sonaruser:sonaruser sonarqube
[root@localhost sonarqube]# su sonaruser
[sonaruser@localhost sonarqube]$ ./sonar.sh start

下载插件
因为官方插件sonarCFamily需要money,所以使用了其他的插件,下载地址https://github.com/SonarOpenCommunity/sonar-cxx,下载放在/usr/local/sonarqube/extensions/plugins/里面,然后重启sonarqube。

安装测试代码环境

1.安装cppcheck

Cppcheck是Cppcheck是一个C/C++代码的静态分析工具。与C++编译器和其他许多分析工具不同,Cppcheck不检测代码中的语法错误,只检测那些编译器通常无法检测到的bug类型,目的是只检测代码中真正的错误。

特点:
检查边界溢出
检查内存泄漏
检查可能的空指针间接引用
检查未初始化的变量
检查无效的STL使用
检查异常安全
警告如果过时的或者不安全的函数使用
警告未使用的或者冗余的代码
检测各种潜在bugs的可疑代码
下载地址http://cppcheck.sourceforge.net/

2.安装rats

RATS 是一个代码安全审计工具,可扫描 C、C++、Perl、PHP 和 Python 源码,检查出一些常见的安全问题,例如缓冲区溢出和 TOCTOU (Time Of Check, Time Of Use) 。
下载地址https://github.com/andrew-d/rough-auditing-tool-for-security

3.安装valgrind

Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合。Valgrind由内核(core)以及基于内核的其他调试工具组成。
内核类似于一个框架(framework),它模拟了一个CPU环境,并提供服务给其他工具;而其他工具则类似于插件 (plug-in),利用内核提供的服务完成各种特定的内存调试任务。
Valgrind包括如下一些工具:
Memcheck:这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
Callgrind:它主要用来检查程序中函数调用过程中出现的问题。
Cachegrind:它主要用来检查程序中缓存使用出现的问题。
Helgrind:它主要用来检查多线程程序中出现的竞争问题。
Massif:它主要用来检查程序中堆栈使用中出现的问题。
Extension:可以利用core提供的功能,自己编写特定的内存调试工具。
下载地址:http://valgrind.org/

4.安装gcovr

gcovr是一个将单元测试中的代码覆盖率以多种方式(包括列表方式、XML文件方式、HTML网页方式等)展示出来的工具。
下载地址:https://pypi.python.org/pypi/gcovr

5.写一个集成这些工具的脚本

Makefile脚本如下:
上半部为编译项目,通过@$(MAKE) -C src执行src目录下的makefile编译项目
下半部为通过cppcheck,rats,valgrind,gcovr工具生成测试扫描代码的报告
alt

6.配置报告路径,执行脚本

配置文件sonar-project.properties

具体如下
sonar.projectKey=ncsrv
sonar.projectName=ncsrv
sonar.projectVersion=1.0

#Paths to source directories.

#Paths are relative to the sonar-project.properties file. Replace “\” by “/“ on Windows.

#Do not put the “sonar-project.properties” file in the same directory with the source code.

#(i.e. never set the “sonar.sources” property to “.”)
sonar.sources=./src

#The value of the property must be the key of the language.
sonar.language=c++

#Encoding of the source code
sonar.sourceEncoding=UTF-8
sonar.cpp.missingIncludeWarnings=true
sonar.cxx.includeDirectories=/usr/include/c++/4.4.4,/usr/include,/home/pas36/include,/usr/local/mysql/include,/usr/include/libxml2/,/usr/include/c++/4.4.4/tr1,/usr/include/linux,src

#paths to the reports
sonar.cxx.cppcheck.reportPath=build/cppcheck-report.xml
sonar.cxx.pclint.reportPath=build/pclint-report.xml
sonar.cxx.coverage.reportPath=build/gcovr-report.xml
sonar.cxx.coverage.itReportPath=build/gcovr-report
.xml
sonar.cxx.coverage.overallReportPath=build/gcovr-report*.xml
sonar.cxx.valgrind.reportPath=build/valgrind-report.xml
sonar.cxx.rats.reportPath=build/rats-report.xml
sonar.cxx.xunit.reportPath=build/xunit-report.xml

然后执行脚本make & make sonar,如果之前执行过,使用make clean清除一下。

看测试结果并根据报告改进代码

http://192.168.20.199:9000,用户密码都是admin