监控与诊断 #

一、监控概述 #

1.1 监控维度 #

text
监控维度:

集群层面
├── 节点状态
├── 数据分布
├── 网络拓扑
└── 集群健康

节点层面
├── CPU使用率
├── 内存使用率
├── 磁盘IO
├── 网络IO
└── JVM状态

应用层面
├── 读写延迟
├── 吞吐量
├── 错误率
└── 连接数

1.2 监控工具 #

text
监控工具:

内置工具
├── nodetool
├── cqlsh tracing
└── 日志文件

外部工具
├── Prometheus + Grafana
├── DataStax OpsCenter
├── Instaclustr
└── 自定义监控

二、nodetool监控 #

2.1 集群状态 #

bash
# 集群状态
nodetool status

# 集群描述
nodetool describecluster

# Gossip信息
nodetool gossipinfo

# 环信息
nodetool ring

2.2 节点信息 #

bash
# 节点详细信息
nodetool info

# 线程池状态
nodetool tpstats

# 网络统计
nodetool netstats

# 代理直方图
nodetool proxyhistograms

2.3 数据统计 #

bash
# 表统计
nodetool tablestats

# 特定表统计
nodetool tablestats my_keyspace.my_table

# 列族统计
nodetool cfstats my_keyspace.my_table

# 数据大小估计
nodetool cfhistograms my_keyspace my_table

2.4 压缩监控 #

bash
# 压缩状态
nodetool compactionstats

# 压缩历史
nodetool compactionhistory

三、性能指标 #

3.1 关键指标 #

text
关键性能指标:

延迟指标
├── ReadLatency:读延迟
├── WriteLatency:写延迟
├── RangeLatency:范围查询延迟
└── P50/P95/P99延迟

吞吐量指标
├── ReadCount:读次数
├── WriteCount:写次数
├── ReadTotalLatency:读总延迟
└── WriteTotalLatency:写总延迟

缓存指标
├── KeyCacheHitRate:键缓存命中率
├── RowCacheHitRate:行缓存命中率
└── CacheSize:缓存大小

压缩指标
├── PendingTasks:待压缩任务数
├── BytesCompacted:已压缩字节数
└── CompactionRate:压缩速率

3.2 表级别指标 #

bash
# 查看表指标
nodetool tablestats my_keyspace.my_table

# 输出示例
Keyspace: my_keyspace
    Table: my_table
    SSTable count: 10
    Space used (live): 1073741824
    Space used (total): 1073741824
    Space used by snapshots: 0
    Memtable cell count: 1000000
    Memtable data size: 104857600
    Memtable switch count: 100
    Read count: 1000000
    Read latency: 0.123 ms
    Write count: 2000000
    Write latency: 0.045 ms
    Pending flushes: 0
    Bloom filter false positives: 100
    Bloom filter false ratio: 0.01
    Bloom filter space used: 1048576
    Index summary space used: 524288
    Key cache hit rate: 0.95

3.3 JVM指标 #

bash
# 查看JVM信息
nodetool info | grep -A 20 "JVM"

# GC日志分析
grep "GC" /var/log/cassandra/gc.log

# 堆内存使用
jstat -gc <pid> 1000

四、日志分析 #

4.1 日志位置 #

text
日志文件位置:

系统日志
├── /var/log/cassandra/system.log
└── 记录系统事件、错误、警告

GC日志
├── /var/log/cassandra/gc.log
└── 记录垃圾回收事件

调试日志
├── /var/log/cassandra/debug.log
└── 详细调试信息

4.2 日志级别配置 #

xml
<!-- logback.xml -->
<configuration>
    <appender name="SYSTEMLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/cassandra/system.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>/var/log/cassandra/system.log.%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
            <maxFileSize>50MB</maxFileSize>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%-5level [%thread] %date{ISO8601} %F:%L - %msg%n</pattern>
        </encoder>
    </appender>
    
    <root level="INFO">
        <appender-ref ref="SYSTEMLOG"/>
    </root>
</configuration>

4.3 日志分析命令 #

bash
# 查看错误
grep -i "error" /var/log/cassandra/system.log

# 查看警告
grep -i "warn" /var/log/cassandra/system.log

# 查看慢查询
grep "ReadTimeout\|WriteTimeout" /var/log/cassandra/system.log

# 实时监控
tail -f /var/log/cassandra/system.log | grep -i "error\|warn"

五、查询追踪 #

5.1 启用追踪 #

sql
-- 启用追踪
TRACING ON;

-- 执行查询
SELECT * FROM users WHERE user_id = ?;

-- 查看追踪结果
-- 显示查询执行的详细信息

5.2 追踪结果分析 #

text
追踪结果包含:

活动信息
├── source:执行节点
├── source_elapsed:耗时
├── thread:执行线程
└── activity:执行活动

分析重点
├── 总耗时
├── 各阶段耗时
├── 网络往返次数
└── 等待时间

六、告警配置 #

6.1 告警规则 #

text
告警规则建议:

严重告警
├── 节点离线
├── 磁盘空间 > 90%
├── 读写延迟 > 100ms
└── GC暂停 > 1s

警告告警
├── 节点离线(单节点)
├── 磁盘空间 > 80%
├── 读写延迟 > 50ms
└── GC暂停 > 500ms

提示告警
├── 压缩队列过长
├── 缓存命中率下降
└── 连接数异常

6.2 监控脚本 #

bash
#!/bin/bash
# cassandra_monitor.sh

# 检查节点状态
check_status() {
    STATUS=$(nodetool status | grep -c "UN")
    TOTAL=$(nodetool status | grep -c "^UN\|^DN")
    
    if [ $STATUS -lt $TOTAL ]; then
        echo "WARNING: Some nodes are down"
        # 发送告警
    fi
}

# 检查磁盘空间
check_disk() {
    USAGE=$(df -h /var/lib/cassandra | tail -1 | awk '{print $5}' | sed 's/%//')
    
    if [ $USAGE -gt 90 ]; then
        echo "CRITICAL: Disk usage is ${USAGE}%"
        # 发送告警
    fi
}

# 检查延迟
check_latency() {
    LATENCY=$(nodetool tablestats | grep "Read latency" | awk '{print $3}')
    
    if [ $(echo "$LATENCY > 100" | bc) -eq 1 ]; then
        echo "WARNING: Read latency is high: ${LATENCY}ms"
        # 发送告警
    fi
}

# 执行检查
check_status
check_disk
check_latency

七、故障诊断 #

7.1 常见问题诊断 #

text
常见问题诊断:

节点离线
├── 检查网络连接
├── 检查防火墙规则
├── 检查Cassandra进程
└── 检查系统日志

读写超时
├── 检查一致性级别
├── 检查网络延迟
├── 检查节点负载
└── 检查GC日志

性能下降
├── 检查压缩状态
├── 检查墓碑数量
├── 检查缓存命中率
└── 检查磁盘IO

7.2 诊断命令 #

bash
# 诊断网络
nodetool gossipinfo
nodetool netstats

# 诊断性能
nodetool proxyhistograms
nodetool cfhistograms my_keyspace my_table

# 诊断压缩
nodetool compactionstats
nodetool compactionhistory

# 诊断线程池
nodetool tpstats

八、总结 #

监控与诊断要点:

  1. nodetool工具:集群和节点状态监控
  2. 性能指标:延迟、吞吐量、缓存命中率
  3. 日志分析:错误、警告、慢查询
  4. 查询追踪:分析查询执行过程
  5. 告警配置:设置合理的告警规则
  6. 故障诊断:快速定位和解决问题

恭喜你完成了Cassandra完全指南的学习!

最后更新:2026-03-27