Skip to content

配置说明

本文档详细介绍 IPAM 系统的配置选项。

配置文件

配置文件使用 TOML 格式,默认路径为 config.toml

配置示例

toml
# 服务器配置
[server]
port = 8080
mode = "release"

# 数据库配置
[database]
type = "mysql"
host = "localhost"
port = 3306
user = "ipam_user"
password = "your_password"
dbname = "ipam"
charset = "utf8mb4"
max_open_conns = 25
max_idle_conns = 5
conn_max_lifetime = 3600

# JWT 配置
[jwt]
secret = "your-secret-key"
expire_hours = 24

# 日志配置
[log]
level = "info"
format = "json"
output = "file"
filename = "logs/ipam.log"
max_size = 100
max_backups = 10
max_age = 30
compress = true

# 监控配置
[monitoring]
enabled = true
interval = 300
max_concurrent = 100
timeout = 5

# 扫描配置
[scanner]
enabled = true
scan_interval = 600
mac_timeout = 5

# 探针配置
[probe]
enabled = true
api_key = "your-probe-api-key"

# 备份配置
[backup]
enabled = true
schedule = "0 2 * * *"
retention_days = 30
output_dir = "backups"

配置项详解

服务器配置 [server]

配置项类型默认值说明
portint8080服务器监听端口
modestring"debug"运行模式:debug/release
read_timeoutint60读取超时(秒)
write_timeoutint60写入超时(秒)

数据库配置 [database]

配置项类型默认值说明
typestring"mysql"数据库类型:mysql/postgres
hoststring"localhost"数据库主机
portint3306数据库端口
userstring-数据库用户名
passwordstring-数据库密码
dbnamestring"ipam"数据库名称
charsetstring"utf8mb4"字符集
max_open_connsint25最大连接数
max_idle_connsint5最大空闲连接数
conn_max_lifetimeint3600连接最大生命周期(秒)

JWT 配置 [jwt]

配置项类型默认值说明
secretstring-JWT 签名密钥
expire_hoursint24Token 过期时间(小时)
refresh_hoursint12Token 刷新时间(小时)

安全提示:生产环境请务必修改默认密钥!

日志配置 [log]

配置项类型默认值说明
levelstring"info"日志级别:debug/info/warn/error
formatstring"json"日志格式:json/text
outputstring"file"输出方式:file/stdout
filenamestring"logs/ipam.log"日志文件路径
max_sizeint100单个日志文件大小(MB)
max_backupsint10保留的备份文件数
max_ageint30日志保留天数
compressbooltrue是否压缩备份

监控配置 [monitoring]

配置项类型默认值说明
enabledbooltrue是否启用监控
intervalint300监控间隔(秒)
max_concurrentint100最大并发数
timeoutint5Ping 超时时间(秒)
retry_countint3重试次数

扫描配置 [scanner]

配置项类型默认值说明
enabledbooltrue是否启用扫描
scan_intervalint600扫描间隔(秒)
mac_timeoutint5MAC 扫描超时(秒)
packet_countint3发送的数据包数量

探针配置 [probe]

配置项类型默认值说明
enabledbooltrue是否启用探针
api_keystring-探针 API 密钥
max_resultsint1000单次上报最大结果数

备份配置 [backup]

配置项类型默认值说明
enabledbooltrue是否启用自动备份
schedulestring"0 2 * * *"备份计划(Cron 表达式)
retention_daysint30备份保留天数
output_dirstring"backups"备份输出目录
compressbooltrue是否压缩备份

环境变量

配置项也可以通过环境变量设置,优先级高于配置文件。

bash
# 数据库配置
export IPAM_DATABASE_HOST=localhost
export IPAM_DATABASE_PORT=3306
export IPAM_DATABASE_USER=ipam_user
export IPAM_DATABASE_PASSWORD=secret
export IPAM_DATABASE_DBNAME=ipam

# JWT 配置
export IPAM_JWT_SECRET=your-secret-key

# 服务器配置
export IPAM_SERVER_PORT=8080
export IPAM_SERVER_MODE=release

多环境配置

支持根据环境加载不同的配置文件:

config.toml          # 默认配置
config.dev.toml      # 开发环境
config.test.toml     # 测试环境
config.prod.toml     # 生产环境

通过环境变量指定环境:

bash
export IPAM_ENV=prod
./ipam-server

配置热加载

部分配置支持热加载,修改后无需重启服务:

  • 日志级别
  • 监控间隔
  • 扫描间隔

发送信号触发重载:

bash
kill -HUP $(pgrep ipam-server)

配置验证

启动时会自动验证配置:

bash
./ipam-server --validate-config

验证内容包括:

  • 配置格式是否正确
  • 必填项是否已填写
  • 数值是否在有效范围内
  • 数据库连接是否正常

常见问题

如何修改监听端口?

toml
[server]
port = 9090

如何关闭监控功能?

toml
[monitoring]
enabled = false

如何调整日志级别?

toml
[log]
level = "debug"  # debug/info/warn/error

如何配置 PostgreSQL?

toml
[database]
type = "postgres"
host = "localhost"
port = 5432
user = "ipam_user"
password = "password"
dbname = "ipam"

基于 MIT 许可发布