diff options
author | 2024-11-15 03:04:06 +0600 | |
---|---|---|
committer | 2024-11-15 03:04:06 +0600 | |
commit | e0139539359c0f3bec41b8ba37438f8026975774 (patch) | |
tree | 362616835d5c3a4fea10738a922933ed6da5e27f /src | |
download | log-parser-e0139539359c0f3bec41b8ba37438f8026975774.tar.gz log-parser-e0139539359c0f3bec41b8ba37438f8026975774.zip |
Initial commit
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.h | 13 | ||||
-rw-r--r-- | src/system.l | 58 | ||||
-rw-r--r-- | src/system.proto | 84 | ||||
-rw-r--r-- | src/system.yy | 90 |
4 files changed, 245 insertions, 0 deletions
diff --git a/src/lexer.h b/src/lexer.h new file mode 100644 index 0000000..c626d02 --- /dev/null +++ b/src/lexer.h @@ -0,0 +1,13 @@ +#ifndef LEXER_H +#define LEXER_H +#if !defined(yyFlexLexerOnce) +#include <FlexLexer.h> +#endif + +template <class T> +class Lexer : public yyFlexLexer { + public: + T yynlex(); +}; + +#endif diff --git a/src/system.l b/src/system.l new file mode 100644 index 0000000..b77255e --- /dev/null +++ b/src/system.l @@ -0,0 +1,58 @@ +%option c++ +%option noyywrap +%option debug +%option yyclass="Lexer" + +%{ +#include <iostream> +#include "system.tab.hh" + +#undef YY_DECL +# define YY_DECL template<> parser::symbol_type Lexer<parser::symbol_type>::yynlex () + +using parser = yy::parser; +using namespace std; +%} + +digit [0-9] +num {digit}+ +frc {digit}+\.{digit}+ +%% +{frc} { return parser::make_FRC(atof(yytext_ptr)); } +{num} { return parser::make_NUM(atoi(yytext_ptr)); } + +[\n]+ { return parser::make_BREAK();} +[ \t]+ { return parser::make_BLANK();} +begin { return parser::make_START(); } +end { return parser::make_END(); } +ver { return parser::make_VERSION(); } +cpu { return parser::make_CPU(); } +base { return parser::make_BASE(); } +user% { return parser::make_USER(); } +nice% { return parser::make_NICE(); } +system% { return parser::make_SYSP(); } +idle% { return parser::make_IDLE(); } +iowait% { return parser::make_IOWP(); } +irq% { return parser::make_IRQP(); } +swirq% { return parser::make_SIRQ(); } +core: { return parser::make_CORE(); } + +mem { return parser::make_MEM(); } +total { return parser::make_TOTAL(); } +cache { return parser::make_CACHE(); } +share { return parser::make_SHARED(); } +tswap { return parser::make_TSWAP(); } +uswap { return parser::make_USWAP(); } + +tmp { return parser::make_TEMP(); } +cur { return parser::make_CURR(); } +min { return parser::make_TMIN(); } +max { return parser::make_TMAX(); } +avg { return parser::make_TAVG(); } + +: { return parser::make_COLON(); } +\/ { return parser::make_SLASH(); } + +. { /* everything else */ } + +%% diff --git a/src/system.proto b/src/system.proto new file mode 100644 index 0000000..eee94a8 --- /dev/null +++ b/src/system.proto @@ -0,0 +1,84 @@ +syntax = "proto3"; +package Entry; + +message System { + uint64 epoch = 1; + CPUStats cpu = 2; + MEMStats mem = 3; + TEMPStats tmp = 4; + repeated Block blks = 5; +} + +message CPUStats { + message Core { + double usage = 1; + fixed32 clock = 2; + } + + message Subsystem { + double diff = 1; + double curr = 2; + } + + Core base = 1; + map<string, Subsystem> subsystem = 2; + map<fixed32, Core> cores = 3; +} + +message MEMStats { + fixed64 total = 1; + fixed64 avail = 2; + fixed64 cache = 3; + fixed64 share = 4; + fixed64 tswap = 5; + fixed64 uswap = 6; +} + +message TEMPStats { + fixed32 cur = 1; + fixed32 min = 2; + fixed32 max = 3; + fixed32 avg = 4; +} + +message Block { + message Part { + enum FSType { + NONE = 0; + EXT2 = 1; + EXT3 = 2; + EXT4 = 3; + NTFS = 4; + FAT32 = 5; + XFS = 6; + BTRFS = 7; + APFS = 8; + HFS_PLUS = 9; + EXFAT = 10; + ZFS = 11; + SWAP = 12; + LVM_PV = 13; + } + + fixed64 size = 1; + fixed64 free = 2; + bool open = 3; + string path = 4; + FSType fmt = 5; + } + + enum TableType { + NONE = 0; + MBR = 1; + GPT = 2; + APM = 3; + BSD = 4; + } + + string name = 1; + TableType fmt = 2; + fixed64 bread = 3; + fixed64 bwrite = 4; + fixed32 parts = 5; + map<fixed32, Part> part = 6; +} diff --git a/src/system.yy b/src/system.yy new file mode 100644 index 0000000..af64edd --- /dev/null +++ b/src/system.yy @@ -0,0 +1,90 @@ +/* parser.y (Bison file) */ +%{ +#define PARSER_SOURCE +#include <fstream> +#include <filesystem> +#include "system.pb.h" // Include the generated protobuf header +#include "lexer.h" +#define yylex lexer.yynlex + +using namespace std; + +Entry::System msg; + +%} + +%language "c++" +%require "3.2" +%define api.token.constructor +%define api.value.type variant + +%code requires // *.hh +{ +#include <cstdint> +#include "lexer.h" +} + +%parse-param {Lexer<symbol_type> &lexer} + +%token <uint64_t> NUM +%token <double> FRC +%token BREAK BLANK START END VERSION CPU BASE USER NICE +%token SYSP IDLE IOWP IRQP SIRQ CORE MEM TOTAL CACHE SHARED +%token TSWAP USWAP TEMP CURR TMIN TMAX TAVG COLON SLASH + +%% + +// Grammar Rules +start: + block + | start block + ; + +block: + block_start data block_end + ; + +block_start: + START BLANK NUM BREAK { + msg.set_epoch($<uint64_t>3); + } + ; + +block_end: + END BLANK NUM BREAK { + if ($<uint64_t>3 != msg.epoch()) { + msg.Clear(); + cout << $<uint64_t>3 << endl; + } else { + cout << $<uint64_t>3 << endl; + auto p = filesystem::path("proto"); + p /= to_string($<uint64_t>3); + p += string(".proto"); + ofstream f(p, ios_base::out | ios_base::binary); + msg.SerializeToOstream(&f); + f.close(); + } + } + +data: +; +%% + +namespace yy +{ + // Report an error to the user. + auto parser::error (const std::string& msg) -> void + { + std::cerr << msg << '\n'; + } +} + +#undef PARSER_SOURCE +#include "lexer.h" + +int main() { + Lexer<yy::parser::symbol_type> lexer; + yy::parser parse(lexer); + + return parse(); +} |