You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typedef struct StatsGlobalContext_ {
/** list of thread stores: one per thread plus one global */
StatsThreadStore *sts;
SCMutex sts_lock;
int sts_cnt;
HashTable *counters_id_hash;
StatsPublicThreadContext global_counter_ctx;
} StatsGlobalContext;
看了一眼这个结构体,大致明白了 sts 是用来存储所有 tv 的 statsCounter 。countsers_id_hash 是 将 tv 索引 到 sts 这个表的下标。但里面这个单独的 global_counter_ctx 就很疑惑了? 继续往下看。。。
suricata 状态监控模块详解
在输出日志stats.log中,suricata会定时输出某些统计信息供分析。这些信息大部分是从多个flow worker线程统计出来的,比如抓包数,解码的包数,tcp flow 或者 udp flow的个数。
下面就来分析一下suricata是如何统计各个线程的状态并输出到日志的。
线程创建模式
执行命令 pidstat -p
pidof suricata
-t 2这些线程分为三种:管理线程,工作线程,控制线程
四个工作线程:
flow worker:W#01,W#02,W#03,W#04线程监听在网卡enp5s0f0。
五个管理线程:
一个控制线程:US (暂时不清楚该线程功能)
CS线程就是负责统计读取各个线程的状态并进行统计输出的。
统计功能模块分析
每个线程以ThreadVar(tv)的实例存在,每个tv内维护一个StatsPublicThreadContext(pctx)和一个StatsPrivateThreadContext(pca)。
线程通过counters模块提供的api: StatsAddUI64, StatsSetUI64, StatsIncr 来对 tv->pca 中的statsLocalCounter进行状态更改。
那么tv中为什么还要维护一个 StatsPublicThreadContext 呢?来看一下 pca 和 pctx 之间的行为。
原来 pca->statsLocalCounter->ctx 就指向着 pctx->statsCounter。tv在修改自身状态时,是去修改 pca->statsLocalCounter 里的内容,并没有去修改 pctx->statsCounter。那如何将修改同步到pctx->statsCounter 呢?
在下面这个函数找到了答案。
所以说,这个 pctx->statsCounter 就是一个单消费者单生产者下的一个读写缓冲区,tv 充当生产者的角色,随时根据自己的状态对 pca 里的 statsLocalCounter 进行 update/set。CW 唤醒线程设置定时闹钟,引发各线程调用 StatsUpdateCounterArray 将状态同步到 pctx->statsCounter。
接下来需要研究 CS 线程如何读取 pctx->statsCounter 并进行统计计算。
看了一眼这个结构体,大致明白了 sts 是用来存储所有 tv 的 statsCounter 。countsers_id_hash 是 将 tv 索引 到 sts 这个表的下标。但里面这个单独的 global_counter_ctx 就很疑惑了? 继续往下看。。。
所以
说的是其中既包括 tv 中的 statsCounter 也包括global_counter_ctx 的 statsCounter。
同理,tv->pctx 也是通过 StatsThreadRegister 这个方法注册到 stats_ctx->sts 中的~
结构都关联起来了,接下来就是研究一下counter模块怎么进行统计计算啦。
counters模块就一个统计输出接口 StatsOutput。看看函数源码还是很容易懂的,不赘述啦~
The text was updated successfully, but these errors were encountered: