Write a command-line program that reads a continuous stream of vmstat-style output from standard input and raises an alert based on a sliding time window.
The program is invoked as:
myscript <metric_name> <threshold> <max_exceed_count> <window_seconds>
Example:
vmstat 1 | myscript us 100 50 300
Requirements:
-
The input begins with a header row containing metric names, such as
us
,
sy
,
id
, and so on.
-
Each subsequent row is one sample collected once per second.
-
Monitor the metric named by
metric_name
.
-
A sample is considered abnormal if the metric value is greater than
threshold
.
-
Maintain a sliding window covering only the most recent
window_seconds
samples.
-
If the number of abnormal samples in the current window becomes greater than
max_exceed_count
, print the abnormal samples from that window.
-
The program should process the stream online without storing unbounded history.
Assume the input is whitespace-delimited and that the target metric always exists in the header.