You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snfLOGmgr.hpp 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // snfLOGmgr.hpp
  2. //
  3. // (C) Copyright 2006 - 2009 ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // SNF Logging and Statistics engine.
  7. ////////////////////////////////////////////////////////////////////////////////
  8. //// Begin snfLOGmgr include only once
  9. #ifndef snfLOGmgr_included
  10. #define snfLOGmgr_included
  11. #include <list>
  12. #include <set>
  13. #include <string>
  14. #include <vector>
  15. #include <sstream>
  16. #include <ctime>
  17. #include <cstdio>
  18. #include "CodeDweller/timing.hpp"
  19. #include "CodeDweller/threading.hpp"
  20. #include "CodeDweller/histogram.hpp"
  21. #include "SNFMulti/snf_match.h"
  22. #include "SNFMulti/snfCFGmgr.hpp"
  23. #include "SNFMulti/snfNETmgr.hpp"
  24. #include "SNFMulti/GBUdb.hpp"
  25. namespace SNFMulti {
  26. class snfNETmgr; // Declare snfNETmgr
  27. extern const char* SNF_ENGINE_VERSION; // Declare the Engine Version Data
  28. //// DiscLogger ////////////////////////////////////////////////////////////////
  29. // Writes log files back to Disc and double buffers data to minimize contention
  30. // and delays. So - if it takes a few milliseconds to post the log to disc, the
  31. // application that post()s to the log does not have to wait. Write back happens
  32. // about once per second when enabled. Files can be appended or overwritten.
  33. class DiscLogger : private CodeDweller::Thread { // Double buffered lazy writer.
  34. private:
  35. CodeDweller::Mutex BufferControlMutex; // Protects buffers while swapping.
  36. CodeDweller::Mutex FlushMutex; // Protects flush operations.
  37. std::string myPath; // Where the file should be written.
  38. std::string BufferA; // Log data buffer A.
  39. std::string BufferB; // Log data buffer B.
  40. bool UseANotB; // Indicates the active buffer.
  41. bool isDirty; // True if data not yet written.
  42. bool isBad; // True if last write failed.
  43. bool isTimeToStop; // True when shutting down.
  44. bool inAppendMode; // True when in append mode.
  45. std::string& FlushingBuffer() { return ((UseANotB)?BufferA:BufferB); } // Returns the buffer for flushing.
  46. std::string& PostingBuffer() { return ((UseANotB)?BufferB:BufferA); } // Returns the buffer for posting.
  47. bool isEnabled; // True when this should run.
  48. void myTask(); // Write back thread task.
  49. public:
  50. DiscLogger(std::string N = "UnNamed"); // Constructs and starts the thread.
  51. ~DiscLogger(); // Flushes and stops the thread.
  52. std::string Path(const std::string PathName) { // Sets the file path.
  53. CodeDweller::ScopeMutex NewSettings(BufferControlMutex);
  54. myPath = PathName;
  55. return myPath;
  56. }
  57. std::string Path() { // Returns the file path.
  58. CodeDweller::ScopeMutex DontMove(BufferControlMutex);
  59. return myPath;
  60. }
  61. bool AppendMode(const bool AppendNotOverwrite) { // Sets append mode if true.
  62. return (inAppendMode = AppendNotOverwrite);
  63. }
  64. bool AppendMode() { return (inAppendMode); } // True if in append mode.
  65. bool OverwriteMode(const bool OverwriteNotAppend) { // Sets overwrite mode if true.
  66. return (inAppendMode = (!OverwriteNotAppend));
  67. }
  68. bool OverwriteMode() { return (!inAppendMode); } // True if in overwrite mode.
  69. void post(const std::string Input, const std::string NewPath = ""); // Post Input to log, [set path].
  70. void flush(); // Flush right now!
  71. bool Bad() { return (isBad); } // True if last write failed.
  72. bool Good() { return (!isBad); } // True if not Bad();
  73. bool Dirty() { return (isDirty); } // True if data needs to be written.
  74. bool Enabled(const bool MakeEnabled) { return (isEnabled = MakeEnabled); } // Enables writing if true.
  75. bool Enabled() { return (isEnabled); } // True if enabled.
  76. const static CodeDweller::ThreadType Type; // The thread's type.
  77. const static CodeDweller::ThreadState DiscLogger_Flush; // Flushing state.
  78. const static CodeDweller::ThreadState DiscLogger_Wait; // Waiting state.
  79. };
  80. //// IPTestRecord //////////////////////////////////////////////////////////////
  81. // Contains a complete analysis of a given IP. snf_RulebaseHandler provides a
  82. // test facility that accepts and processes IPTestRecord objects. The calling
  83. // process can then submit the IPTestRecord along with it's action to the
  84. // snfLOGmgr for logging.
  85. class IPTestRecord { // IP Analysis Record.
  86. public:
  87. CodeDweller::IP4Address IP; // The IP to be tested.
  88. GBUdbRecord G; // The GBUdb Record for the IP.
  89. snfIPRange R; // The GBUdb classification (range).
  90. int Code; // Code associated with Range.
  91. IPTestRecord(CodeDweller::IP4Address testIP) : IP(testIP), Code(0) {} // Construct with an IP.
  92. };
  93. //// snfScanData ///////////////////////////////////////////////////////////////
  94. // Contains testing data for a message.
  95. // It's defined here in the LOGmgr module because this is the module that must
  96. // log and collect statistics for each scanned message. The snfScanData object
  97. // is the standardized way each engine reports it's scan results to snfLOGmgr.
  98. const int MaxIPsPerMessage = 50; // Maximum number of IPs to scan per message.
  99. struct IPScanRecord { // Structure for IP scan results.
  100. int Ordinal; // Which IP starting with zero.
  101. unsigned int IP; // What is the IP.
  102. GBUdbRecord GBUdbData; // GBUdb data.
  103. };
  104. class snfScanData { // Scan Data record for each message.
  105. private:
  106. IPScanRecord MyIPScanData[MaxIPsPerMessage]; // Array of IP scan results.
  107. int MyIPCount; // Count of IP scan results.
  108. bool DrillDownFlags[MaxIPsPerMessage]; // DrillDown flags. (Set Ignore).
  109. int SourceIPOrdinal; // Ordinal to source IP scan data.
  110. bool SourceIPFoundFlag; // True if source IP is set.
  111. snfIPRange SourceIPRangeFlag; // GBUdb detection range for source IP.
  112. CodeDweller::IP4Address myCallerForcedSourceIP; // Caller forced source IP if not 0UL.
  113. CodeDweller::IP4Address myHeaderDirectiveSourceIP; // Header forced source IP if not 0UL.
  114. public:
  115. snfScanData(int ScanHorizon); // Constructor.
  116. ~snfScanData(); // Destructor.
  117. // The ReadyToClear bit helps multi-phase input situations where the first
  118. // phase might add some input data before calling the base-level scanner.
  119. // In those cases, the pre-scan-phase will clear() the ScanData (and with
  120. // it the ReadyToClear bit) before adding a few critical pieces of data -
  121. // such as the scan name and the scan-start UTC for example. When the base
  122. // level scanner is called to perform the actual scan, the clear() call
  123. // will be inert so that any pre-set data will be preserved.
  124. bool ReadyToClear; // True when Logging is done.
  125. void clear(); // Clear for a new message.
  126. class NoFreeIPScanRecords {}; // Thrown when we run out of scan records.
  127. class OutOfBounds {}; // Thrown in IPScanData if no record at i.
  128. int IPScanCount(); // Return the number of IPs.
  129. IPScanRecord& newIPScanRecord(); // Get the next free IP scan record.
  130. IPScanRecord& IPScanData(int i); // Return the IP scan record i.
  131. // 20080221 _M We can now define in header directives patterns for Received
  132. // headers that we should drill past if they show up as a message source
  133. // candidate. This allows GBUdb to learn to ignore certain IPs automatically
  134. // as they arrive either by IP stubs such as "[12.34.56." or by reverse DNS
  135. // data such as "friendly.example.com [". When the header directives engine
  136. // scans the headers it will call drillPastOrdinal for any Received header
  137. // that matches a <drilldown/> directive. Later when the header analysis
  138. // engine tries to pick the source for the message it will check each source
  139. // candidate against the isDrillDownSource() method. If the source is to be
  140. // ignored then it will set the ignore flag for that IP, process it as if
  141. // it were ignored, and continue searching for the actual source.
  142. void drillPastOrdinal(int O); // Sets Drill Down flag for IP record O.
  143. bool isDrillDownSource(IPScanRecord& X); // True if we drill through this source.
  144. CodeDweller::IP4Address HeaderDirectiveSourceIP(CodeDweller::IP4Address A); // set Header directive source IP.
  145. CodeDweller::IP4Address HeaderDirectiveSourceIP(); // get Header directive source IP.
  146. CodeDweller::IP4Address CallerForcedSourceIP(CodeDweller::IP4Address A); // set Caller forced source IP.
  147. CodeDweller::IP4Address CallerForcedSourceIP(); // get Caller forced source IP.
  148. IPScanRecord& SourceIPRecord(IPScanRecord& X); // Sets the source IP record.
  149. IPScanRecord& SourceIPRecord(); // Gets the source IP record.
  150. bool FoundSourceIP(); // True if the source IP record was set.
  151. snfIPRange SourceIPRange(); // GET Source IP range.
  152. snfIPRange SourceIPRange(snfIPRange R); // SET Source IP range for this scan.
  153. // Direct access data...
  154. std::string SourceIPEvaluation; // GBUdb Source IP evaluation.
  155. // LogControl and General Message Flags
  156. time_t StartOfJobUTC; // Timestamp at start of job.
  157. int SetupTime; // Time in ms spent setting up to scan.
  158. std::string ScanName; // Identifying name or message file name.
  159. CodeDweller::Timer ScanTime; // Scan time in ms.
  160. int ScanDepth; // Scan Depth in evaluators.
  161. std::string ClassicLogText; // Classic log entry text if any.
  162. std::string XMLLogText; // XML log entry text if any.
  163. std::string XHDRsText; // XHeaders text if any.
  164. bool XHeaderInjectOn; // True if injecting headers is on.
  165. bool XHeaderFileOn; // True if creating .xhdr file is on.
  166. bool MessageFileTypeCGPOn; // Expect a CGP type message file.
  167. unsigned int ScanSize; // What size is the scan request.
  168. // GBUdb Activity Flags
  169. bool GBUdbNormalTriggered; // True if GBUdb indeterminate IP source.
  170. bool GBUdbWhiteTriggered; // True if GBUdb found source IP white.
  171. bool GBUdbWhiteSymbolForced; // True if white was on and symbol was set.
  172. bool GBUdbPatternSourceConflict; // True if pattern was found with white IP.
  173. bool GBUdbAutoPanicTriggered; // True if autopanic was triggered.
  174. bool GBUdbAutoPanicExecuted; // True if an autopanic was added.
  175. bool GBUdbBlackTriggered; // True if GBUdb found source IP black.
  176. bool GBUdbBlackSymbolForced; // True if black was on and symbol was set.
  177. bool GBUdbTruncateTriggered; // True if Truncate was possible.
  178. bool GBUdbPeekTriggered; // True if we could peek.
  179. bool GBUdbSampleTriggered; // True if we could sample.
  180. bool GBUdbTruncateExecuted; // True if we actually did truncate.
  181. bool GBUdbPeekExecuted; // True if we peeked instead of truncating.
  182. bool GBUdbSampleExecuted; // True if we sampled.
  183. bool GBUdbCautionTriggered; // True if GBUdb found source IP suspicous.
  184. bool GBUdbCautionSymbolForced; // True if caution was on and symbol was set.
  185. // Rule panics
  186. std::set<int> RulePanics; // A list of rule IDs panicked this scan.
  187. // Pattern Engine Scan Result Data
  188. std::vector<unsigned char> FilteredData; // Message data after filter chain.
  189. unsigned long int HeaderDirectiveFlags; // Flags set by header directives.
  190. bool PatternWasFound; // True if the pattern engine matched.
  191. int PatternID; // The winning rule ID.
  192. int PatternSymbol; // The associated symbol.
  193. std::list<snf_match> MatchRecords; // List of match records.
  194. std::list<snf_match>::iterator MatchRecordsCursor; // Localized iterator for match records.
  195. int MatchRecordsDelivered; // Match records seen so far.
  196. int CompositeFinalResult; // What the scan function returned.
  197. };
  198. //// SMHDMY counter
  199. //
  200. // Provides a running SUM for a series of sliding windows. The input() expects
  201. // a new piece of data every second (or so). It is presumed that another counter
  202. // will keep track of the actual milliseconds if accuracy is required. The object
  203. // is all primative data parts so it is possible to store and retrieve this object
  204. // in binary format on the same system when that's helpful.
  205. class snf_SMHDMY_Counter { // Sliding window "live" counter.
  206. private:
  207. bool do_input(int X, int& SUM, int* DATA, int& ORDINAL, int SIZE); // Subroutine for assimilating input.
  208. public:
  209. snf_SMHDMY_Counter() { // When making a new one, reset all
  210. memset(this, 0, sizeof(snf_SMHDMY_Counter)); // data to zero. It's all ints ;-)
  211. }
  212. // 60 seconds is a minute (6 x 10)
  213. int SEC6DATA[6], SEC6SUM, SEC6ORDINAL;
  214. int SEC10DATA[10], SEC10SUM, SEC10ORDINAL;
  215. // 60 minutes is an hour (6 x 10)
  216. int MIN6DATA[6], MIN6SUM, MIN6ORDINAL;
  217. int MIN10DATA[10], MIN10SUM, MIN10ORDINAL;
  218. // 24 hours is a day (4 x 6)
  219. int HOUR4DATA[4], HOUR4SUM, HOUR4ORDINAL;
  220. int HOUR6DATA[6], HOUR6SUM, HOUR6ORDINAL;
  221. // 7 days is a week (7)
  222. int WEEK7DATA[7], WEEK7SUM, WEEK7ORDINAL;
  223. // 30 days is a month (5 x 6)
  224. int MONTH5DATA[5], MONTH5SUM, MONTH5ORDINAL;
  225. int MONTH6DATA[6], MONTH6SUM, MONTH6ORDINAL;
  226. // 12 months (almost) is a year (3 x 4)
  227. int YEAR3DATA[3], YEAR3SUM, YEAR3ORDINAL;
  228. int YEAR4DATA[4], YEAR4SUM, YEAR4ORDINAL;
  229. // 365 days is a year
  230. int YEAR365DATA[365], YEAR365SUM, YEAR365ORDINAL;
  231. void input(int X); // Add new data to the counter.
  232. bool Cycled60Seconds() { return (0 == SEC6ORDINAL && 0 == SEC10ORDINAL); } // Full cycle of data for seconds.
  233. int Sum60Seconds() { return SEC10SUM; }
  234. int Sum66Seconds() { return (SEC6SUM + SEC10SUM); }
  235. int SumThru1Minute() { return Sum66Seconds(); } // All samples thru one minute.
  236. bool Cycled60Minutes() { // Full cycle of data for minutes.
  237. return (Cycled60Seconds() && 0 == MIN6ORDINAL && 0 == MIN10ORDINAL);
  238. }
  239. int Sum60Minutes() { return MIN10SUM; }
  240. int Sum66Minutes() { return (MIN6SUM + MIN10SUM); }
  241. int SumThru1Hour() { return SumThru1Minute() + Sum66Minutes(); } // All samples thru one hour.
  242. bool Cycled24Hours() { // Full cycle of data for hours.
  243. return (Cycled60Minutes() && 0 == HOUR4ORDINAL && 0 == HOUR6ORDINAL);
  244. }
  245. int Sum24Hours() { return HOUR6SUM; }
  246. int Sum28Hours() { return (HOUR4SUM + HOUR6SUM); }
  247. int SumThru1Day() { return SumThru1Hour() + Sum28Hours(); } // All samples thru one day.
  248. bool Cycled7Days() { return (Cycled24Hours() && 0 == WEEK7ORDINAL); } // Full cycle of data for week.
  249. int Sum7Days() { return WEEK7SUM; }
  250. int SumThru1Week() { return SumThru1Day() + Sum7Days(); } // All samples thru one week.
  251. bool Cycled30Days() { // Full cycle of data for month.
  252. return (Cycled24Hours() && 0 == MONTH6ORDINAL && 0 == MONTH5ORDINAL);
  253. }
  254. int Sum30Days() { return MONTH6SUM; }
  255. int Sum35Days() { return (MONTH5SUM + MONTH6SUM); }
  256. int SumThru1Month() { return SumThru1Day() + Sum35Days(); } // All samples thu one month.
  257. bool Cycled12Months() { // Full cycle of data for 12 months.
  258. return (Cycled30Days() && 0 == YEAR3ORDINAL && 0 == YEAR4ORDINAL);
  259. }
  260. int Sum450Days() { return (YEAR3SUM + YEAR4SUM); }
  261. int SumThru1Year() { return SumThru1Month() + Sum450Days(); } // All samples thru one year.
  262. bool Cycled365Days() { return (Cycled24Hours() && 0 == YEAR365ORDINAL); } // Full cycle of data for 365 days.
  263. int Sum365Days() { return YEAR365SUM; }
  264. };
  265. //// snfLOGmgr /////////////////////////////////////////////////////////////////
  266. // A note about the LOG manager and configuration data:
  267. // Events that are logged with the log manager may come from scans using
  268. // different configurations. In order to keep things as sane as possible,
  269. // operations that are dependent on configuration information such as creating
  270. // log file entries or producing status page data will require that an
  271. // appropriate snfCFGData object be provided by reference and that the
  272. // snfCFGData object be guaranteed to remain stable for the duration of the
  273. // call. Changing snfCFGData may result in inconsistent results.
  274. //
  275. // This requirement is fairly easy to accomplish since posts to the LOGmgr
  276. // will come from scanning engines that have a snfCFGPacket "grab()ed" during
  277. // their operations, and executive requests will come from the ruelbase
  278. // manager which can grab a snfCFGPacket for the duration of the request.
  279. const int NumberOfResultCodes = 64;
  280. class snfCounterPack {
  281. public:
  282. snfCounterPack(); // Construct new CounterPacks clean.
  283. void reset(); // How to reset a counter pack.
  284. CodeDweller::Timer ActiveTime; // Measures Active (swapped in) Time.
  285. struct {
  286. unsigned long Scans; // Number of messages scanned.
  287. unsigned long Spam; // Count of spam results.
  288. unsigned long Ham; // Count of ham results.
  289. unsigned long GBUdbNormalTriggered; // Count of indeterminate gbudb IP hits.
  290. unsigned long GBUdbWhiteTriggered; // Count of GBUdb found source IP white.
  291. unsigned long GBUdbWhiteSymbolForced; // Count of white was on and symbol was set.
  292. unsigned long GBUdbPatternSourceConflict; // Count of pattern was found with white IP.
  293. unsigned long GBUdbAutoPanicTriggered; // Count of autopanic was triggered.
  294. unsigned long GBUdbAutoPanicExecuted; // Count of an autopanic was added.
  295. unsigned long GBUdbBlackTriggered; // Count of GBUdb found source IP black.
  296. unsigned long GBUdbBlackSymbolForced; // Count of black was on and symbol was set.
  297. unsigned long GBUdbTruncateTriggered; // Count of Truncate was possible.
  298. unsigned long GBUdbPeekTriggered; // Count of we could peek.
  299. unsigned long GBUdbSampleTriggered; // Count of we could sample.
  300. unsigned long GBUdbTruncateExecuted; // Count of if we actually did truncate.
  301. unsigned long GBUdbPeekExecuted; // Count of we peeked instead of truncating.
  302. unsigned long GBUdbSampleExecuted; // Count of we sampled.
  303. unsigned long GBUdbCautionTriggered; // Count of GBUdb found source IP suspicous.
  304. unsigned long GBUdbCautionSymbolForced; // Count of caution was on and symbol was set.
  305. unsigned long PatternWasFound; // Count of scanner matches.
  306. unsigned long RulePanicFound; // Count of rule panics.
  307. } Events;
  308. };
  309. //// Interval timers precisely track the time between hack()s. There are
  310. //// two timers inside. One is active, the other is stopped. Each time hack()
  311. //// is called, one timer becomes active at the moment the other is stopped.
  312. class IntervalTimer { // Precision interval timer.
  313. private:
  314. CodeDweller::Timer A; // Here is one timer.
  315. CodeDweller::Timer B; // Here is the other timer.
  316. bool ANotB; // True if A is the active timer.
  317. CodeDweller::Timer& Active(); // Selects the active timer.
  318. CodeDweller::Timer& Inactive(); // Selects the inactive timer.
  319. public:
  320. CodeDweller::msclock hack(); // Chop off a new interval & return it.
  321. CodeDweller::msclock Interval(); // Return the last interval.
  322. CodeDweller::msclock Elapsed(); // Return the time since last hack.
  323. };
  324. //// PersistentState stores the counters we keep between runs.
  325. class snfLOGPersistentState {
  326. public:
  327. snfLOGPersistentState() :
  328. Ready(0),
  329. LastSyncTime(0),
  330. LastSaveTime(0),
  331. LastCondenseTime(0),
  332. LatestRuleID(0),
  333. SerialNumberCounter(0) {}
  334. bool Ready; // True if we're ready to use.
  335. void store(std::string& FileNameToStore); // Write the whole thing to a file.
  336. void restore(std::string& FileNameToRestore); // Read the whole thing from a file.
  337. time_t LastSyncTime; // time_t of last Sync event.
  338. time_t LastSaveTime; // time_t of last GBUdb Save event.
  339. time_t LastCondenseTime; // time_t of last GBUdb Condense event.
  340. int LatestRuleID; // Latest rule ID seen so far.
  341. int SerialNumberCounter; // Remembers the serial number.
  342. };
  343. class snfLOGmgr : private CodeDweller::Thread {
  344. private:
  345. CodeDweller::Mutex MyMutex; // Mutex to serialize updates & queries.
  346. CodeDweller::Mutex ConfigMutex; // Mutex to protect config changes.
  347. CodeDweller::Mutex SerialNumberMutex; // Protects the serial number.
  348. CodeDweller::Mutex PeekMutex; // Protects Peek Loop Counter.
  349. CodeDweller::Mutex SampleMutex; // Protects Sample Loop Counter.
  350. CodeDweller::Mutex StatusReportMutex; // Protects status report post & get.
  351. snfCounterPack CounterPackA, CounterPackB; // Swapable counter packs.
  352. snfCounterPack* CurrentCounters; // Current Event Counters.
  353. snfCounterPack* ReportingCounters; // Counters being used to collect data.
  354. snfCounterPack* getSnapshot(); // Get a copy of the current counters.
  355. volatile bool Configured; // True if we're properly configured.
  356. volatile bool TimeToDie; // True when the thread should stop.
  357. volatile int PeekEnableCounter; // How many peek attempts recently?
  358. volatile int SampleEnableCounter; // How many sample attempts recently?
  359. void myTask(); // Thread task.
  360. time_t StartupTime; // Time since engine started.
  361. snfLOGPersistentState Status; // Persistent State Data.
  362. std::string PersistentFileName; // File name for the State Data.
  363. snfNETmgr* myNETmgr; // Net manager link.
  364. GBUdb* myGBUdb; // GBUdb link.
  365. // Configuration
  366. std::string ActiveRulebaseUTC; // UTC of last successful load.
  367. std::string AvailableRulebaseUTC; // UTC of rulebase available for update.
  368. bool NewerRulebaseIsAvailable; // True if a newer rulebase is available.
  369. std::string myPlatformVersion; // Version info for platform.
  370. bool Rotate_LocalTime; // Rotate logs using localtime.
  371. std::string LogsPath; // Path to logs directory.
  372. bool ClassicLogRotate; // True = Rotate Classic Log.
  373. bool XMLLogRotate; // True = Rotate XML Log.
  374. // Live stats
  375. snf_SMHDMY_Counter MessageCounter;
  376. snf_SMHDMY_Counter HamCounter;
  377. snf_SMHDMY_Counter SpamCounter;
  378. snf_SMHDMY_Counter WhiteCounter;
  379. snf_SMHDMY_Counter CautionCounter;
  380. snf_SMHDMY_Counter BlackCounter;
  381. snf_SMHDMY_Counter TruncateCounter;
  382. snf_SMHDMY_Counter SampleCounter;
  383. snf_SMHDMY_Counter AutoPanicCounter;
  384. snf_SMHDMY_Counter RulePanicCounter;
  385. snf_SMHDMY_Counter TimeCounter;
  386. // Histograms
  387. CodeDweller::Histogram ResultsSecond;
  388. CodeDweller::Histogram ResultsMinute;
  389. CodeDweller::Histogram ResultsHour;
  390. CodeDweller::Histogram RulesSecond;
  391. CodeDweller::Histogram RulesMinute;
  392. CodeDweller::Histogram RulesHour;
  393. CodeDweller::Histogram PanicsSecond;
  394. CodeDweller::Histogram PanicsMinute;
  395. CodeDweller::Histogram PanicsHour;
  396. // Reporting
  397. std::string NodeId; // We need this for our status msgs.
  398. void do_StatusReports(); // Update & sequence status reports.
  399. int XML_Log_Mode; // What is the XML log mode.
  400. int Classic_Log_Mode; // What is the Classic log mode.
  401. // Every second we get the basics and collect data. (local only)
  402. bool SecondReport_Log_OnOff;
  403. bool SecondReport_Append_OnOff;
  404. std::string SecondReport_Log_Filename;
  405. std::string SecondReportText;
  406. std::string SecondReportTimestamp;
  407. bool do_SecondReport(); // Send our 1 second status report.
  408. // Every minute we get hard data and event logs. (for sync)
  409. bool MinuteReport_Log_OnOff;
  410. bool MinuteReport_Append_OnOff;
  411. std::string MinuteReport_Log_Filename;
  412. std::string MinuteReportText;
  413. std::string MinuteReportTimestamp;
  414. CodeDweller::Histogram PatternRulesHistogram;
  415. bool do_MinuteReport(); // Send our 1 minute status report.
  416. // Every hour we get a summary.
  417. bool HourReport_Log_OnOff;
  418. bool HourReport_Append_OnOff;
  419. std::string HourReport_Log_Filename;
  420. std::string HourReportText;
  421. std::string HourReportTimestamp;
  422. bool do_HourReport(); // Send our 1 hour status report.
  423. void postStatusLog( // Post a Status log if required.
  424. const std::string& LogData, // Here's the log entry's data.
  425. const std::string& LogFileName, // Here is where it should go.
  426. const bool LogEnabled, // This is true if we should write it.
  427. const bool AppendNotOverwrite, // True=Append, False=Overwrite.
  428. DiscLogger& Logger // Lazy Log Writer to use.
  429. );
  430. DiscLogger SecondStatusLogger; // Lazy writer for Second status.
  431. DiscLogger MinuteStatusLogger; // Lazy writer for Minute status.
  432. DiscLogger HourStatusLogger; // Lazy writer for Hour status.
  433. DiscLogger XMLScanLogger; // Lazy writer for XML Scan log.
  434. DiscLogger ClassicScanLogger; // Lazy writer for Classic Scan log.
  435. void doXHDRs(snfCFGData& CFGData, snfScanData& ScanData); // XHDR sub routine for LogThisScan()
  436. void doXMLLogs(snfCFGData& CFGData, snfScanData& ScanData); // XML sub routine for LogThisScan()
  437. void doClassicLogs(snfCFGData& CFGData, snfScanData& ScanData); // Classic sub routine for LogThisScan()
  438. void captureLTSMetrics(snfCFGData& CFGData, snfScanData& ScanData); // LogThisScan section 1, Locked.
  439. void performLTSLogging(snfCFGData& CFGData, snfScanData& ScanData); // LogThisScan section 2, Unlocked.
  440. public:
  441. snfLOGmgr(); // Initialize & start the thread.
  442. ~snfLOGmgr(); // Stop the thread & clean up.
  443. void stop(); // Stops the manager.
  444. void linkNETmgr(snfNETmgr& N); // Link in my NETmgr
  445. void linkGBUdb(GBUdb& G); // Link in my GBUdb
  446. void configure(snfCFGData& CFGData); // Update the configuration.
  447. void updateActiveUTC(std::string ActiveUTC); // Set active rulebase UTC.
  448. void logThisIPTest(IPTestRecord& I, std::string Action); // Capthre the data from an IP test.
  449. void logThisScan(snfCFGData& CFGData, snfScanData& ScanData); // Capture the data from this scan.
  450. void logThisError(snfScanData& ScanData, const std::string ContextName, // Inject an error log entry for this
  451. const int Code, const std::string Text // scan using this number & message.
  452. );
  453. void logThisError(std::string ContextName, int Code, std::string Text); // Log an error message.
  454. void logThisInfo(std::string ContextName, int Code, std::string text); // Log an informational message.
  455. std::string PlatformVersion(std::string NewPlatformVersion); // Set platform version info.
  456. std::string PlatformVersion(); // Get platform version info.
  457. std::string EngineVersion(); // Get engine version info.
  458. void updateAvailableUTC(std::string& AvailableRulebaseTimestamp); // Stores Available, true==update ready.
  459. std::string ActiveRulebaseTimestamp(); // Get active rulebase timestamp.
  460. std::string AvailableRulebaseTimestamp(); // Get available rulebase timestamp.
  461. bool isUpdateAvailable(); // True if update is available.
  462. bool OkToPeek(int PeekOneInX); // Check to see if it's ok to peek.
  463. bool OkToSample(int SampleOneInX); // Check to see if it's ok to sample.
  464. time_t Timestamp(); // Get an ordinary timestamp.
  465. std::string Timestamp(time_t t); // Convert time_t to a timestamp s.
  466. std::string& Timestamp(std::string& s); // Appends a current timestamp in s.
  467. std::string LocalTimestamp(time_t t); // Convert time_t to a local timestamp s.
  468. std::string& LocalTimestamp(std::string& s); // Appends a current local timestamp in s.
  469. unsigned int SerialNumber(); // Returns the next serial number.
  470. std::string& SerialNumber(std::string& s); // Appends the next serial number.
  471. int SecsSinceStartup(); // Gets seconds since starup.
  472. void RecordSyncEvent(); // Sets timestamp of latest Sync.
  473. int SecsSinceLastSync(); // Gets seconds since latest Sync.
  474. void RecordSaveEvent(); // Sets timestamp of latest Save.
  475. int SecsSinceLastSave(); // Gets seconds since latest Save.
  476. void RecordCondenseEvent(); // Sets timestamp of latest Condense.
  477. int SecsSinceLastCondense(); // Gets seconds since latest Condense.
  478. // Live stats functions
  479. double MessagesPerMinute(); // Avg Msgs/Minute.
  480. double HamPerMinute(); // Avg Ham/Minute.
  481. double SpamPerMinute(); // Avg Spam/Minute.
  482. double WhitePerMinute(); // Avg White/Minute.
  483. double CautionPerMinute(); // Avg Caution/Minute.
  484. double BlackPerMinute(); // Avg Black/Minute.
  485. double TruncatePerMinute(); // Avg Truncate/Minute.
  486. double SamplePerMinute(); // Avg Sample/Minute.
  487. int LatestRuleID(); // Returns the latest Rule ID seen.
  488. int RunningTime(); // Seconds running since startup.
  489. std::string getStatusSecondReport(); // Get latest status.second report.
  490. std::string getStatusMinuteReport(); // Get latest status.minute report.
  491. std::string getStatusHourReport(); // Get latest status.hour report.
  492. const static CodeDweller::ThreadType Type; // The thread's type.
  493. };
  494. #include "snfLOGmgr.inline.hpp"
  495. } // namespace SNFMulti
  496. #endif
  497. //// End snfLOGmgr include only once
  498. ////////////////////////////////////////////////////////////////////////////////