likes
comments
collection
share

夜莺监控V6在MAC环境中二进制方式搭建

作者站长头像
站长
· 阅读数 25

前言

前些天用云服务使用二进制方式构建过一次夜莺v6的版本,不过还想再折腾一下,打算开始重新在mac本地环境上再搭建一下。你可以获得到的知识点包含:

  • mac查看硬件信息
  • Homebrew方式安装mariadb
  • 查看端口占用-lsof命令
  • 启动项管理-launchctl命令
  • Homebrew方式安装redis
  • VictoriaMetrics单机版安装
  • go环境安装以及vscode配置
  • 夜莺n9e编译安装
  • Categraf采集器编译安装

二进制方式构建

查看系统信息

system_profiler SPHardwareDataType

Hardware:

Hardware Overview:

  Model Name: MacBook Pro
  Model Identifier: MacBookPro18,3
  Model Number: x
  Chip: Apple M1 Pro
  Total Number of Cores: 8 (6 performance and 2 efficiency)
  Memory: 16 GB
  System Firmware Version: 8422.100.650
  OS Loader Version: 8422.100.650
  Serial Number (system): x
  Hardware UUID: x
  Provisioning UDID: x
  Activation Lock Status: x

uname -a

Darwin xxdeMacBook-Pro.local 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64	

sysctl machdep

machdep.cpu.cores_per_package: 8
machdep.cpu.core_count: 8
machdep.cpu.logical_per_package: 8
machdep.cpu.thread_count: 8
machdep.cpu.brand_string: Apple M1 Pro
machdep.user_idle_level: 0
machdep.wake_abstime: 5768706646558
machdep.time_since_reset: 135506147099
machdep.wake_conttime: 15606192945512
machdep.deferred_ipi_timeout: 64000
machdep.virtual_address_size: 47
machdep.report_phy_read_delay: 0
machdep.report_phy_write_delay: 0
machdep.trace_phy_read_delay: 0
machdep.trace_phy_write_delay: 0
machdep.report_phy_read_osbt: 0
machdep.report_phy_write_osbt: 0
machdep.phy_read_delay_panic: 0
machdep.phy_write_delay_panic: 0

数据库安装-mariadb

这里默认已经安装了Homebrew,如果没有请参考文末链接安装

brew info mariadb
==> mariadb: stable 10.11.3 (bottled)
Drop-in replacement for MySQL
https://mariadb.org/
Conflicts with:
mariadb-connector-c (because both install `mariadb_config`)
mysql (because mariadb, mysql, and percona install the same binaries)
mytop (because both install `mytop` binaries)
percona-server (because mariadb, mysql, and percona install the same binaries)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mariadb.rb
License: GPL-2.0-only
==> Dependencies
Build: bison ✘, cmake ✘, fmt ✘, pkg-config ✘
Required: groonga ✘, openssl@1.1 ✘, pcre2 ✔, zstd ✘
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To restart mariadb after an upgrade:
brew services restart mariadb
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mariadb/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

在下一步运行brew install时候发现不成:),按提示去安装了xcode

brew install mariadb
Error: No developer tools installed.
Install the Command Line Tools:
xcode-select --install

安装xcode

xcode-select --install
xcode-select: note: install requested for command line developer tools

在次尝试install

brew install mariadb
...
To restart mariadb after an upgrade:
brew services restart mariadb
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mariadb/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

查看安装状态

brew services list

Name    Status User File
mariadb none

brew services info mariadb

mariadb (homebrew.mxcl.mariadb)
Running: ✘
Loaded: ✘
Schedulable: ✘

启动mysql-不建议(1)

mysql.server start
Starting MariaDB
230516 16:03:46 mysqld_safe Logging to '/opt/homebrew/var/mysql/bogon.err'.
230516 16:03:46 mysqld_safe Starting mariadbd daemon with databases from /opt/homebrew/var/mysql
. SUCCESS!

ps -eaf|grep mysql

501 67191     1   0  4:03下午 ttys001    0:00.02 /bin/sh /opt/homebrew/Cellar/mariadb/10.11.3/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql --pid-file=/opt/homebrew/var/mysql/bogon.pid
501 67265 67191   0  4:03下午 ttys001    0:00.07 /opt/homebrew/Cellar/mariadb/10.11.3/bin/mariadbd --basedir=/opt/homebrew/Cellar/mariadb/10.11.3 --datadir=/opt/homebrew/var/mysql --plugin-dir=/opt/homebrew/Cellar/mariadb/10.11.3/lib/plugin --log-error=/opt/homebrew/var/mysql/bogon.err --pid-file=/opt/homebrew/var/mysql/bogon.pid
501 67291 18006   0  4:04下午 ttys006    0:00.00 grep mysql

mysql.server stop

ERROR! MariaDB server process #67265 is not running!

mysql.server stop

ERROR! MariaDB server PID file could not be found!

ps -eaf|grep mysql

501 67191     1   0  4:03下午 ttys001    0:00.02 /bin/sh /opt/homebrew/Cellar/mariadb/10.11.3/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql --pid-file=/opt/homebrew/var/mysql/bogon.pid
501 67265 67191   0  4:03下午 ttys001    0:00.12 /opt/homebrew/Cellar/mariadb/10.11.3/bin/mariadbd --basedir=/opt/homebrew/Cellar/mariadb/10.11.3 --datadir=/opt/homebrew/var/mysql --plugin-dir=/opt/homebrew/Cellar/mariadb/10.11.3/lib/plugin --log-error=/opt/homebrew/var/mysql/bogon.err --pid-file=/opt/homebrew/var/mysql/bogon.pid
501 67465 18006   0  4:08下午 ttys006    0:00.00 grep mysql

kill -9 67265 67191

此方式可以启动mysql,但是stop不了,也可能是我姿势不对,欢迎指出。

启动mysql-建议(2) 随系统启动

brew services start mariadb

==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)

brew services info mariadb

mariadb (homebrew.mxcl.mariadb)
Running: ✔
Loaded: ✔
Schedulable: ✘
User: xx
PID: 68712

此时可以在通过命令查看添加的启动项

brew services list

Name    Status  User File
mariadb started x ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

launchctl list|grep mariadb
69646	0	homebrew.mxcl.mariadb

停止并移除启动项

brew services stop mariadb 

Stopping `mariadb`... (might take a while)
==> Successfully stopped `mariadb` (label: homebrew.mxcl.mariadb)

同样还可以用run(仅启动不注册,目前不是很懂什么意思),man中是这样描述:

[sudo] brew services run (formula|--all):
    Run the service formula without registering to launch at login (or boot).

[sudo] brew services start (formula|--all|--file=):
    Start the service formula immediately and register it to launch at login
(or boot).

目前观察到这两者区别是写plist位置不同,user有无,欢迎大佬指出

brew services run mariadb

==> Successfully ran `mariadb` (label: homebrew.mxcl.mariadb)

brew services list

Name    Status  User File
mariadb started      /opt/homebrew/opt/mariadb/homebrew.mxcl.mariadb.plist

这里我就不换端口了接下来,配置文件可通过命令查到 mysql --help | grep /my.cnf,现在给MariaDB设置用户及密码

lsof -i4TCP:3306
COMMAND    PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mariadbd 76287 xx   21u  IPv6 0x65040a38c4bc9f2d      0t0  TCP *:mysql (LISTEN)
mariadbd 76287 xx   22u  IPv4 0x65040a33f990bd55      0t0  TCP *:mysql (LISTEN)

初始化mariadb

mysql_install_db

WARNING: The host 'bogon' could not be looked up with /opt/homebrew/Cellar/mariadb/10.11.3/bin/resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MariaDB version. The MariaDB daemon, mariadbd, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MariaDB privileges !
mysql.user table already exists!
Run mysql_upgrade, not mysql_install_db

再执行初始化脚步,这里我第一次没加sudo,一直错误。。所以一定记得sudo

mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Enter current password for root (enter for none):

这是加了sudo执行的结果,大家按需配置

sudo mysql_secure_installation
Password:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n
... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

连接数据库,我上面设置了密码1234

 mysql -uroot -p1234 -e "show databases"
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+

安装redis

直接用homebrew安装redis

brew install redis  

...
==> Running `brew cleanup redis`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

启动

brew services start redis
==> Successfully started `redis` (label: homebrew.mxcl.redis)

./bin/redis-server& ./redis.conf

查看redis状态

ps -aef | grep redis

501 80129     1   0  7:05下午 ??         0:00.10 /opt/homebrew/opt/redis/bin/redis-server 127.0.0.1:6379
501 80171 18006   0  7:06下午 ttys006    0:00.00 grep redis   

lsof -i:6379

COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
redis-ser 80129 xx    6u  IPv4 0x65040a33f9bc9c25      0t0  TCP localhost:6379 (LISTEN)
redis-ser 80129 xx    7u  IPv6 0x65040a38c4ba7f2d      0t0  TCP localhost:6379 (LISTEN)

安装 TSDB-单机版VictoriaMetrics

下载VictoriaMetrics

mkdir ~/Tarball

cd ~/Tarball

brew install wget

wget https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.90.0/victoria-metrics-darwin-arm64-v1.90.0.tar.gz

mkdir victoria-metrics

tar xf victoria-metrics-darwin-arm64-v1.90.0.tar.gz -C victoria-metrics

cd victoria-metrics

启动

nohup ./victoria-metrics-prod &>victoria.log &

查看默认端口8428

lsof -i  tcp |grep victoria
victoria- 81943 xx   12u  IPv4 0x65040a33f9a7c865      0t0  TCP *:8428 (LISTEN)

安装夜莺

mac版本的夜莺,在快猫的官网我目前没有看到,要自行编译,我这里假设认为大家和我一样也没go环境,我再记录一下vscode搭建环境的过程

brew install go

go env

下载vscode https://code.visualstudio.com/

打开vscode,快捷键command+shift+x,输入关键字go,安装 Go 扩展,安装完后再用快捷键command+shift+p,输入关键字Go: Install/Update tools,全部工具都安装

接着去github下载夜莺源码 https://github.com/ccfos/nightingale

wget https://github.com/ccfos/nightingale/archive/refs/tags/v6.0.0-ga.7.0.2.tar.gz
mkdir n9e_source
tar zxvf v6.0.0-ga.7.0.2.tar.gz -C n9e_source

打算通过Makefile运行,很不幸没成功,需要git的参数,也就是不能直接下载sourcecode来编译

make
致命错误:不是 git 仓库(或者任何父目录):.git
go build -ldflags "-w -s -X github.com/ccfos/nightingale/v6/pkg/version.Version="-"" -o n9e ./cmd/center/main.go

所以要么换成git来拉取代码,或者另一种取巧方式是下载已经打好的包,手动打包替换二进制程序,我这里选择后一种。

cd ~/Tarball
wget https://download.flashcat.cloud/n9e-v6.0.0-ga.7.0.1-linux-arm64.tar.gz
mkdir n9e
tar zxvf n9e-v6.0.0-ga.7.0.1-linux-arm64.tar.gz -C n9e

通过vscode去打开夜莺项目,可以很容易在项目根目录下的Makefile文件中找到编译命令 go build -ldflags "-w -s -X github.com/ccfos/nightingale/v6/pkg/version.Version=$(RELEASE_VERSION)" -o n9e ./cmd/center/main.go 根据这个稍微改一下参数因为加了会让不好调试

设置编译参数 -ldflags "-w -s"。其中-w为去掉调试信息(无法使用gdb调试),-s为去掉符号表(暂未清楚具体作用)经过修改的编译命令如下, 其中参数GOOS,GOARCH默认是匹配当前架构的可以不用主动加,交叉编译时候可以必须加

cd ~/Tarball/n9e_source/nightingale-6.0.0-ga.7.0.2/
GOOS=darwin GOARCH=arm64 go build -o n9e ./cmd/center/main.go
mv ./n9e ~/Tarball/n9e/n9e

编译完成后就会在当前目录出现一个名为n9e的程序。剩下n9e-pushgw,n9e-alert,n9e-cli类似替换,做完这些就完成了编译全部过程。

GOOS=darwin GOARCH=arm64 go build -o n9e-alert ./cmd/alert/main.go
mv ./n9e-alert ~/Tarball/n9e/n9e-alert
GOOS=darwin GOARCH=arm64 go build -o n9e-pushgw ./cmd/pushgw/main.go
mv ./n9e-pushgw ~/Tarball/n9e/n9e-pushgw
GOOS=darwin GOARCH=arm64 go build -o n9e-cli ./cmd/cli/main.go
mv ./n9e-cli ~/Tarball/n9e/n9e-cli

在启动之前,需要找有些配置操作, 就像社区里 提议 一样,我觉得现在方式确实有点稍微麻烦,不利于新手入门,希望可以更简单上手

  1. 导入数据库

    mysql -uroot -p <n9e.sql
    
  2. 修改 N9e 的配置文件 (需要注意上线前修改密钥Auth相关字段)

     vim etc/config.toml
     	
     [DB]
     # postgres: host=%s port=%s user=%s dbname=%s password=%s sslmode=%s
     DSN="root:1234@tcp(127.0.0.1:3306)/n9e_v6?charset=utf8mb4&parseTime=True&loc=Local&allowNativePasswords=true" #更换db密码,端口
     
     [[Pushgw.Writers]]
     # Url = "http://127.0.0.1:8480/insert/0/prometheus/api/v1/write"
     Url = "http://127.0.0.1:8428/api/v1/write" #更换为vm的端口
     			
    

启动n9e服务

nohup ./n9e > n9e.log 2>&1 &

[1] 90304

lsof -i4tcp:17000

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
n9e     90304 xx   11u  IPv6 0x65040a38c4bae72d      0t0  TCP *:17000 (LISTEN)

浏览器访问http://localhost:17000,然后输入用户名root,密码root.2020即可登录系统。

添加数据源

一登录就提示‘暂无数据源配置,请联系管理员去添加数据源’ ,去配置数据源(系统配置-数据源),VM(victoria-metrics),所以选择添加Prometheus Like

名称为 xxxVM

URL为vm的地址 http://localhost:8428

write_addr为vm远程写地址http://localhost:8428/api/v1/write

write_addr就是用于推送数据给TSDB。

关联告警引擎集群为 default,还需这个配置它和告警生成有关。不过据我观察官方问答网站里,因为未关联告警引擎集群造成没有告警生成的情况还是挺常见的。

安装Categraf

接下来我们要利用Categraf采集信息推送给n9e,n9e将数据再转发给vm。其中为什么不是直接推给vm,其主要作用是为了解析出机器列表(基础设施-机器列表),好对这些机器进行管理(获取机器架构,时间偏移等元数据,状态监控,告警自愈)。

下载catgraf,https://github.com/flashcatcloud/categraf/ 同上n9e,也是准备下载源码,打好包,替换二进制

cd ~/Tarball/
wget https://github.com/flashcatcloud/categraf/releases/download/v0.3.3/categraf-v0.3.3-linux-arm64.tar.gz
wget https://github.com/flashcatcloud/categraf/archive/refs/tags/v0.3.3.tar.gz
mkdir categraf_source categraf
tar xf v0.3.3.tar.gz -C categraf_source
tar xf categraf-v0.3.3-linux-arm64.tar.gz -C categraf
cd ~/Tarball/categraf_source/categraf-0.3.3
GOOS=darwin GOARCH=arm64 go build -o categraf
mv ./categraf ~/Tarball/categraf/categraf-v0.3.3-linux-arm64/categraf

修改配置文件

cd ~/Tarball/categraf/categraf-v0.3.3-linux-arm64
vim conf/config.toml

[[writers]]
url = "http://127.0.0.1:17000/prometheus/v1/write"

[heartbeat]
enable = true

启动catgraf

nohup ./categraf &>categraf.log &

这样完成了夜莺搭建,再通过WebUI查询数据,配置告警规则来告警判断等等操作

最后感谢看完,由于作者水平有限,使用很多工具并不熟悉,如有错误和遗漏欢迎指出,感谢谅解。

参考资料

Mac OS 查看系统版本信息/硬件信息的命令

在Mac OS系统下查看CPU型号以及核心数量

MAC安装MariaDB详细步骤

2023最新总结,Mac下使用Homebrew完全指南

Homebrew介绍和使用以及xcode-select –install安装失败不能安装该软件

MAC安装、启动、初始化、完整卸载Mariadb(MySQL)

在 Mac 上的“终端”中使用 launchd 管理脚本

使用brew services管理服务

详解lsof命令

MAC安装、启动、初始化、完整卸载Mariadb(MySQL)

Redis—苹果Mac电脑os系统安装Redis步骤

为 Go 开发配置Visual Studio Code

go build -ldflags 参数及 gdb 调试

Linux nohup、&、 2>&1是什么?

Linux如何在vim里搜索关键字

转载自:https://juejin.cn/post/7233952159662653499
评论
请登录