tabin-plugins 0.2.3

Libs for building nagios-compatible check scripts, some scripts, and some libs to read from /proc and /sys on Linux.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![cfg_attr(feature="clippy", allow(if_not_else))]

#[macro_use]
extern crate clap;
extern crate chrono;
extern crate hyper;
extern crate itertools;
extern crate url;
extern crate rustc_serialize;

extern crate tabin_plugins;

use std::cmp::max;
use std::error::Error;
use std::fmt;
use std::io::{self, Read};
use std::str::FromStr;
use std::time::Duration;
use std::thread::sleep;

use chrono::naive::datetime::NaiveDateTime;
use hyper::error::Error as HyperError;
use itertools::Itertools;
use rustc_serialize::json::{self, Json};

use tabin_plugins::Status;

/// One of the datapoints that graphite has returned.
///
/// Graphite always returns all values in its time range, even if it hasn't got
/// any data for them, so the val might not exist.
#[derive(Debug, PartialEq, Clone)]
struct DataPoint {
    val: Option<f64>,
    time: NaiveDateTime
}

impl<'a> From<&'a Json> for DataPoint {
    /// Convert a [value, timestamp] json list into a datapoint
    /// Exits the process with a critical status if data is malformed.
    fn from(point: &Json) -> DataPoint {
        DataPoint {
            val: match point[0] {
                Json::Null => None,
                Json::F64(n) => Some(n),
                Json::U64(n) => Some(n as f64),
                Json::I64(n) => Some(n as f64),
                _ => {
                    println!("Unable to convert data value into floating point: {:?}", point[0]);
                    Status::Critical.exit();
                }
            },
            time: if let Json::U64(n) = point[1] {
                NaiveDateTime::from_timestamp(n as i64, 0)
            } else {
                println!("Timestamp does not look like an integer: {:?}", point[1]);
                Status::Critical.exit();
            }
        }
    }
}

impl fmt::Display for DataPoint {
    #[cfg_attr(feature="clippy", allow(float_cmp))]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} (at {})",
               self.val.map_or("null".into(),
                               |v| format!("{:.*}",
                                           // the number of points past the dot to show
                                           // Don't show any if it's an integer
                                           if v.round() == v { 0 } else { 2 },
                                           v)),
               self.time.format("%H:%Mz"))
    }
}

/// All the data for one fully-resolved target
#[derive(PartialEq, Debug)]
struct GraphiteData {
    points: Vec<DataPoint>,
    target: String
}

/// Represent the data that we received after some filtering operation
#[derive(Debug, PartialEq)]
struct FilteredGraphiteData<'a> {
    original: &'a GraphiteData,
    points: Vec<&'a DataPoint>
}

impl GraphiteData {
    fn from_json_obj (obj: &Json) -> GraphiteData {
        let dl = obj
            .find("datapoints").expect("Could not find datapoints in obj")
            .as_array().expect("Graphite did not return an array").into_iter()
            .map(DataPoint::from)
            .collect();
        GraphiteData {
            points: dl,
            target: obj.find("target").expect("Couldn't find target in graphite data")
                       .as_string().expect("Couldn't convert target to string").to_owned()
        }
    }

    /// References to the points that exist and do not satisfy the comparator
    // comparator is a box closure, which is not allows in map_or
    #[cfg_attr(feature="clippy", allow(redundant_closure))]
    fn invalid_points(&self, comparator: &Box<Fn(f64) -> bool>) -> Vec<&DataPoint> {
        self.points.iter()
            .filter(|p| p.val.map_or(false, |v| comparator(v))).collect()
    }

    // comparator is a box closure, which is not allows in map_or
    #[cfg_attr(feature="clippy", allow(redundant_closure))]
    fn last_invalid_points(&self, n: usize, comparator: &Box<Fn(f64) -> bool>) -> Vec<&DataPoint> {
        self.points.iter()
            .rev()
            .filter(|p| p.val.is_some())
            .take(n)
            .filter(|p| p.val.map_or(false, |v| comparator(v))).collect()
    }
}

impl<'a> FilteredGraphiteData<'a> {
    /// The number of points that we have
    fn len(&self) -> usize {
        self.points.len()
    }

    /// If there are any points in the filtered graphite data
    fn is_empty(&self) -> bool {
        self.points.is_empty()
    }

    /// The percent of the original points that were included by the filter
    ///
    /// This only includes the original points that actually have data
    fn percent_matched(&self) -> f64 {
        (self.len() as f64 /
         self.original.points.iter().filter(|point| point.val.is_some()).count() as f64) * 100.0
    }
}

struct GraphiteIterator {
    current: usize,
    back: usize,
    data: GraphiteData
}

impl Iterator for GraphiteIterator {
    type Item = DataPoint;
    fn next(&mut self) -> Option<DataPoint> {
        self.current += 1;
        self.data.points.get(self.current - 1).cloned()
    }
}

impl IntoIterator for GraphiteData {
    type Item = DataPoint;
    type IntoIter = GraphiteIterator;
    fn into_iter(self) -> Self::IntoIter {
        GraphiteIterator {
            current: 0,
            back: self.points.len(),
            data: self
        }
    }
}

impl DoubleEndedIterator for GraphiteIterator {
    fn next_back(&mut self) -> Option<DataPoint> {
        self.back -= 1;
        self.data.points.get(self.back).cloned()
    }
}

fn graphite_result_to_vec(data: &Json) -> Vec<GraphiteData> {
    data.as_array().expect("Graphite should return an array").iter()
        .map(GraphiteData::from_json_obj).collect()
}

enum GraphiteError {
    HttpError(HyperError),
    JsonError(String),
    IoError(String),
}

impl GraphiteError {
    fn short_display(&self) -> String {
        match *self {
            GraphiteError::HttpError(ref e) => e.description().to_owned(),
            GraphiteError::JsonError(_) => "Error parsing json".to_owned(),
            GraphiteError::IoError(_) => "Error reading stream from graphite".to_owned(),
        }
    }
}

impl From<HyperError> for GraphiteError {
    fn from(e: HyperError) -> Self {
        GraphiteError::HttpError(e)
    }
}

impl From<io::Error> for GraphiteError {
    fn from(e: io::Error) -> Self {
        GraphiteError::IoError(e.description().to_owned())
    }
}

impl fmt::Display for GraphiteError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            GraphiteError::HttpError(ref e) => e.fmt(f),
            GraphiteError::JsonError(ref e) => write!(f, "{}", e),
            GraphiteError::IoError(ref e) => write!(f, "{}", e),
        }
    }
}

/// Fetch data from graphite
///
/// Returns a tuple of (full request path, string that graphite returned), or an error
#[cfg_attr(test, allow(dead_code))]
fn get_graphite(url: &str, target: &str, window: i64, print_url: bool, graphite_error: &Status)
-> Result<GraphiteResponse, GraphiteError> {
    let full_path = format!("{}/render?target={}&format=json&from=-{}min",
                            url, target, window);
    let c = hyper::Client::new();
    if print_url {
        println!("INFO: querying {}", full_path);
    }
    let mut result = try!(c.get(&full_path).send());
    let mut s = String::new();
    try!(result.read_to_string(&mut s));
    match json::Json::from_str(&s) {
        Ok(data) => Ok(GraphiteResponse { result: data, url: result.url.clone() }),
        Err(e) => match e {
            json::ParserError::SyntaxError(..) => {
                Err(GraphiteError::JsonError(format!(
                    "{}: Graphite returned invalid json:\n{}\n\
                     =========================\n\
                     The full url queried was: {}",
                            graphite_error, s, result.url)))
            },
            _ => {
                Err(GraphiteError::JsonError(format!("{}: {}", graphite_error, e)))
            }
        }
    }
}

struct GraphiteResponse {
    result: Json,
    url: url::Url
}

/// Load data from graphite
///
/// Retry until success or exit the script
fn fetch_data(url: &str,
              target: &str,
              window: i64,
              retries: u8,
              graphite_error: &Status,
              print_url: bool)
-> Result<GraphiteResponse, String> {
    let mut attempts = 0;
    let mut retry_sleep = 2000;
    loop {
        match get_graphite(url, target, window, print_url, graphite_error) {
            Ok(s) => {
                return Ok(s);
            },
            Err(e) => {
                print!("Error for {}: {}. ", url, e.short_display());
                if attempts < retries {
                    println!("Retrying in {}s.", retry_sleep / 1000);
                    attempts += 1;
                    sleep(Duration::from_millis(retry_sleep));
                    retry_sleep *= 2;
                    continue;
                } else {
                    println!("\nFull error: {}", e);
                    println!("Giving up after {} attempts.", retries + 1);
                    graphite_error.exit();
                }
            }
        };
    }
}

/// Take an operator and a value and return a function that can be used in a
/// filter to return only values that *do not* satisfy the operator.
///
/// aka a function that returns only invalid numbers
///
/// Note: JSON considers all numbers either floats or arbitrary-precision
/// decimals, so we use f64. Comparing f64 directly equal with each other is dangerous.
///
/// We aren't ever actually doing math on floats, so even though decimal 0.1 !=
/// float 0.1, since we are always interpreting as f64 then we'll get the
/// "wrong" values and compare them to each other. That said, we should
/// probably use an epsilon in here.
#[cfg_attr(feature="clippy", allow(float_cmp))]
fn operator_string_to_func(op: &str, op_is_negated: NegOp, val: f64) -> Box<Fn(f64) -> bool> {
    let comp: Box<Fn(f64) -> bool> = match op {
        "<"  => Box::new(move |i: f64| i <  val),
        "<=" => Box::new(move |i: f64| i <= val),
        ">"  => Box::new(move |i: f64| i >  val),
        ">=" => Box::new(move |i: f64| i >= val),
        "==" => Box::new(move |i: f64| i == val),
        "!=" => Box::new(move |i: f64| i != val),
        a => panic!("Bad operator: {}", a)
    };

    if op_is_negated == NegOp::Yes {
        Box::new(move |i: f64| !comp(i))
    } else {
        comp
    }
}

/// Take a `Json` value and make sure that at least one series has real data
fn filter_to_with_data(path: &str,
                       data: Json,
                       no_data_status: Status) -> Result<Vec<GraphiteData>, Status> {
    let data = graphite_result_to_vec(&data);
    let matched_len = data.len();
    if data.is_empty() {
        println!("{}: Graphite returned no matching series for pattern '{}'",
                 no_data_status, path);
        return Err(no_data_status);
    }
    let series_with_data = data.into_iter()
        .filter(|series|
                !series.points.is_empty() &&
                !{
                    let null_points = series.points.iter().fold(
                        0, |count, point|{
                            if point.val.is_none() {
                                count + 1
                            } else {
                                count
                            }
                        });
                    null_points == series.points.len()
                })
        .collect::<Vec<GraphiteData>>();

    if series_with_data.is_empty() {
        println!("{}: Graphite found {} series but returned only null datapoints for them",
                 no_data_status, matched_len);
        Err(no_data_status)
    } else {
        Ok(series_with_data)
    }
}

fn do_check(
    series_with_data: &[GraphiteData],
    op: &str,
    op_is_negated: NegOp,
    threshold: f64,
    error_condition: PointAssertion,
    status: Status
) -> Status {
    let comparator = operator_string_to_func(op, op_is_negated, threshold);
    // We want to create a vec of series' that only have (existing) invalid
    // points. The first element is the original length of the vector of points
    let with_invalid = match error_condition {
        // Here, invalid points can exist anywhere
        PointAssertion::Ratio(error_ratio) => series_with_data.iter()
            .map(|series| FilteredGraphiteData {
                original: &series,
                points: series.invalid_points(&comparator)
            })
            .filter(|invalid| {
                if error_ratio == 0.0 {
                    !invalid.points.is_empty()
                } else {
                    let filtered = invalid.points.len() as f64;
                    let original = invalid.original.points.len() as f64;
                    filtered / original >= error_ratio
                }
            })
            .collect::<Vec<FilteredGraphiteData>>(),
        PointAssertion::Recent(count) => series_with_data.iter()
            .map(|ref series| FilteredGraphiteData {
                original: &series,
                points: series.last_invalid_points(count, &comparator)
            })
            .filter(|ref invalid| !invalid.is_empty())
            .collect::<Vec<(FilteredGraphiteData)>>()
    };

    let nostr = if op_is_negated == NegOp::Yes { " not" } else { "" };
    if !with_invalid.is_empty() {
        match error_condition {
            PointAssertion::Ratio(ratio) => {
                if series_with_data.len() == with_invalid.len() {
                    if with_invalid.len() == 1 {
                        print!("{}: ", status)
                    } else if ratio == 0.0 {
                        println!("{}: All {} matched paths have invalid datapoints:",
                                 status, with_invalid.len())
                    } else {
                        println!(
                            "{}: All {} matched paths have at least {:.0}% invalid datapoints:",
                            status, with_invalid.len(), ratio * 100.0)
                    }
                } else {
                    println!("{}: Of {} paths with data, \
                             {} have at least {:.1}% invalid datapoints:",
                         status, series_with_data.len(), with_invalid.len(), ratio * 100.0);
                }
                for series in &with_invalid {
                    let prefix = if with_invalid.len() == 1 {
                        ""
                    } else {
                        "       ->"
                    };
                    println!(
                        "{} {} has {} points ({:.1}%) that are{} {} {}: {}",
                        prefix,
                        series.original.target, series.points.len(),
                        series.percent_matched(),
                        nostr, op, threshold,
                        series.points.iter().map(|gv| format!("{}", gv))
                            .join(", "));
                }
            },
            PointAssertion::Recent(count) => {
                println!("{}: Of {} paths with data, {} have the last {} points invalid:",
                         status, series_with_data.len(), with_invalid.len(), count);
                for series in &with_invalid {
                    let descriptor = if count == 1 {
                        "point is"
                    } else {
                        "points are"
                    };
                    println!(
                        "       -> {} last {} {}{} {} {}: {}",
                        series.original.target, count, descriptor, nostr, op, threshold,
                        series.points.iter().map(|gv| format!("{}", gv))
                            .join(", "));
                }
            }
        }
        Status::Critical
    } else {
        match error_condition {
            PointAssertion::Ratio(percent) => {
                let amount;
                if percent == 0.0 { amount = "any".to_owned() } else { amount = format!("at least {:.1}% of", percent * 100.0) }
                println!(
                    "OK: Found {} paths with data, none had {} datapoints{} {} {:.2}.",
                    series_with_data.len(), amount, nostr, op, threshold);
            },
            PointAssertion::Recent(count) => {
                println!(
                    "OK: Found {} paths with data, none had their last {} datapoints{} {} {}.",
                    series_with_data.len(), count, nostr, op, threshold
                    );
            }
        };
        for series in series_with_data.iter() {
            println!("    -> {}: {}",
                     series.target,
                     series.points.iter()
                     .map(|gv| format!("{}", gv))
                     .join(", "))
        }
        Status::Ok
    }
}

struct Args {
    url: String,
    path: String,
    assertions: Vec<Assertion>,
    window: i64,
    retries: u8,
    graphite_error: Status,
    no_data: Status,
    print_url: bool,
}

static ASSERTION_EXAMPLES: &'static [&'static str] = &[
    "critical if any point is > 0",
    "critical if any point in at least 40% of series is > 0",
    "critical if any point is not > 0",
    "warning if any point is == 9",
    "critical if all points are > 100.0",
    "critical if at least 20% of points are > 100",
    "critical if most recent point is > 5",
    "critical if most recent point in all series are == 0",
    ];

fn parse_args() -> Args {
    let allowed_no_data = Status::str_values(); // block-local var for borrowck
    let args = clap::App::new("check-graphite")
        .version("0.1.0")
        .author("Brandon W Maister <quodlibetor@gmail.com>")
        .about("Query graphite and exit based on predicates")
        .args_from_usage(
            "<URL>                 'The domain to query graphite. Must include scheme (http/s)'
             <PATH>                'The graphite path to query. For example: \"collectd.*.cpu\"'
             <ASSERTION>...        'The assertion to make against the PATH. See Below.'
             -w --window=[MINUTES] 'How many minutes of data to test. Default 10.'
             --retries=[COUNT]     'How many times to retry reaching graphite. Default 4.
             --print-url           'Unconditionally print the graphite url queried'
             --verify-assertions   'Just check assertion syntax, do not query urls'")
        .arg(clap::Arg::with_name("NO_DATA_STATUS")
                       .long("--no-data")
                       .help("What to do with no data.
                              Choices: ok, warn, critical, unknown.
                              This is the value to use for the assertion 'if all values are null'
                              Default: warn.")
                       .takes_value(true)
                       .possible_values(&allowed_no_data)
             )
        .arg(clap::Arg::with_name("GRAPHITE_ERROR_STATUS")
                       .long("--graphite-error")
                       .help("What to do with no data.
                              Choices: ok, warn, critical, unknown.
                              What to say if graphite returns a 500 or invalid JSON
                              Default: unknown.")
                       .takes_value(true)
                       .possible_values(&allowed_no_data)
             )
        .after_help(&format!("About Assertions:

    Assertions look like 'critical if any point in any series is > 5'.

    They describe what you care about in your graphite data. The structure of
    an assertion is as follows:

        <errorkind> if <point spec> [in <series spec>] is|are [not] <operator> <threshold>

    Where:

        - `errorkind` is either `critical` or `warning`
        - `point spec` can be one of:
            - `any point`
            - `all points`
            - `at least <N>% of points`
            - `most recent point`
        - `series spec` (optional) can be one of:
            - `any series`
            - `all series`
            - `at least <N>% of series`
        - `not` is optional, and inverts the following operator
        - `operator` is one of: `==` `!=` `<` `>` `<=` `>=`
        - `threshold` is a floating-point value (e.g. 100, 78.0)

    Here are some example assertions:

        - `{}`\n", ASSERTION_EXAMPLES.join("`\n        - `")))
     .get_matches();

    let assertions = args.values_of("ASSERTION").unwrap()
        .iter().map(|assertion_str|
                    match parse_assertion(assertion_str) {
                        Ok(a) => a,
                        Err(e) => {
                            println!("Error `{}` in assertion `{}`", e, assertion_str);
                            Status::Critical.exit();
                        }
                    }).collect();

    if args.is_present("verify-assertions") {
        Status::Ok.exit();
    }


    Args {
        url: args.value_of("URL").unwrap().to_owned(),
        path: args.value_of("PATH").unwrap().to_owned(),
        assertions: assertions,
        window: value_t!(args.value_of("MINUTES"), i64).unwrap_or(10),
        retries: value_t!(args.value_of("COUNT"), u8).unwrap_or(4),
        graphite_error: Status::from_str(args.value_of("GRAPHITE_ERROR_STATUS")
                                         .unwrap_or("unknown")).unwrap(),
        no_data: Status::from_str(args.value_of("NO_DATA_STATUS")
                                  .unwrap_or("warning")).unwrap(),
        print_url: args.is_present("print-url"),
    }
}

#[derive(Debug, PartialEq)]
struct Assertion {
    operator: String,
    op_is_negated: NegOp,
    threshold: f64,
    point_assertion: PointAssertion,
    series_ratio: f64,
    failure_status: Status
}

enum AssertionState {
    /// Deciding if breaking this assertion means we go critical or warning
    Status,
    /// We're about to describe an assertion over points
    Points,
    /// We're about to describe an assertion over series
    Series,
    /// We're looking for an operator
    Operator,
    /// We're looking for a threshold
    Threshold,
    /// Unknown state
    Open
}

#[derive(Debug)]
enum ParseError {
    NoPointSpecifier(String),
    NoSeriesSpecifier(String),
    InvalidOperator(String),
    InvalidThreshold(String),
    NoRatioSpecifier(String),
    NoStatusSpecifier(String),
    SyntaxError(String)
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ParseError::*;
        let msg = match *self {
            NoPointSpecifier(ref msg) |
            NoSeriesSpecifier(ref msg) |
            InvalidOperator(ref msg) |
            InvalidThreshold(ref msg) |
            NoRatioSpecifier(ref msg) |
            NoStatusSpecifier(ref msg) |
            SyntaxError(ref msg) => msg
        };
        write!(f, "{}", msg)
    }
}

#[derive(Debug, PartialEq)]
enum PointAssertion {
    Ratio(f64),
    Recent(usize)
}

/// convert "all" -> 1, "at least 70% (points|series)" -> 0.7
fn parse_ratio<'a, 'b, I>(it: &'b mut I, word: &str) -> Result<PointAssertion, ParseError>
    where I: Iterator<Item=&'a str>
{
    use PointAssertion::*;
    let ratio;

    // chew through
    //   "any"
    //   "at least NN% of"

    if word == "any" {
        ratio = Ok(Ratio(0.0));
    } else if word == "all" {
        ratio = Ok(Ratio(1.0))
    } else if word == "at" {
        let mut rat = None;
        while let Some(word) = it.next() {
            if word == "least" { /* 'at least' */ }
            else if word.find('%') == Some(word.len() - 1) {
                rat = word[..word.len() - 1].parse::<f64>().ok();
                break;
            } else if word == "points" || word == "point" {
                return Err(ParseError::NoPointSpecifier(
                    format!("Expected ratio specifier before '{}'", word)));
            } else if word == "series" {
                return Err(ParseError::NoSeriesSpecifier(
                    format!("Expected ratio specifier before '{}'", word)));
            } else {
                return Err(ParseError::NoRatioSpecifier(
                    format!("This shouldn't happen: {}, word.find('%'): {:?}, len: {}",
                            word, word.find('%'), word.len())));
            }
        }
        ratio = Ok(Ratio(rat.expect("Couldn't find ratio for blah") / 100f64))
    } else if word == "most" {
        match it.next() {
            Some(word) if word == "recent" => { /* yay */ },
            Some(word) => return Err(ParseError::SyntaxError(
                format!("Expected 'most recent' found 'most {}'",
                        word))),
            None => return Err(ParseError::SyntaxError(
                "Expected 'most recent' found trailing 'most'".to_owned()))
        };
        match it.next() {
            Some(word) if word == "point" => return Ok(Recent(1)),
            Some(word) => return Err(ParseError::SyntaxError(
                format!("Expected 'most recent point' found 'most recent {}'",
                        word))),
            None => return Err(ParseError::SyntaxError(
                "Expected 'most recent point' found trailing 'most recent'".to_owned()))
        }
    } else {
        ratio = Err(ParseError::SyntaxError(
            format!("Expected 'any', 'all', 'most' or 'at least', found '{}'", word)))
    }

    if ratio.is_ok() {
        // chew stop words
        for word in it {
            // chew through terminators
            if word == "of" { continue; }
            else if word == "points" || word == "point" || word == "series" {
                break;
            }
            else {
                return Err(ParseError::SyntaxError(
                    format!("Expected 'of points|series', found '{}'", word)))
            }
        }
    }

    ratio
}

// Whether or not the operator in the assertion is negated
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NegOp {
    // For situations like `are not`
    Yes,
    // Just `are`
    No
}

fn parse_assertion(assertion: &str) -> Result<Assertion, ParseError> {
    let mut state = AssertionState::Status;
    let mut operator: Option<&str> = None;
    let mut threshold: Option<f64> = None;
    let mut status = None;
    let mut point_assertion = None;
    let mut series_ratio = 0.0;
    let mut it = assertion.split(' ').peekable();
    let mut negated: NegOp = NegOp::No;

    while let Some(word) = it.next() {
        match state {
            AssertionState::Status => {
                status = match word {
                    "critical" => Some(Status::Critical),
                    "warning" => Some(Status::Warning),
                    _ => return Err(ParseError::NoStatusSpecifier(format!(
                        "Expect assertion to start with 'critical' or 'warning', not '{}'", word)))
                };
                if let Some(next) = it.next() {
                    if next != "if" {
                        return Err(ParseError::SyntaxError(format!(
                                "Expected 'if' to follow '{}', found '{}'", word, next)));
                    }
                } else {
                    return Err(ParseError::SyntaxError(format!(
                                "Unexpected end of input after '{}'", word)));
                }
                state = AssertionState::Points;
            },
            AssertionState::Points => {
                point_assertion = Some(try!(parse_ratio(&mut it, word)));
                state = AssertionState::Open;
            },
            AssertionState::Open => {
                if word == "in" {
                    state = AssertionState::Series
                } else if word == "is" || word == "are" {
                    if it.peek() == Some(&"not") {
                        negated = NegOp::Yes;
                        it.next();
                    }
                    state = AssertionState::Operator
                } else {
                    return Err(ParseError::SyntaxError(
                        format!("Expected 'in' or 'is'/'are' (series spec or operator), found '{}'", word)))
                }
            },
            AssertionState::Series => {
                if let PointAssertion::Ratio(r) = try!(parse_ratio(&mut it, word)) {
                    series_ratio = r;
                } else {
                    return Err(ParseError::SyntaxError(
                        "You can't specify a most recent series, \
                         it doesn't make sense.".to_owned()));
                }
                state = AssertionState::Open;
            },
            AssertionState::Operator => {
                if word == "be" {}
                else {
                    if let Some(word) = ["<", "<=", ">", ">=", "==", "!="].iter()
                                        .find(|&&op| op == word) {
                        operator = Some(word);
                        state = AssertionState::Threshold;
                    } else {
                        return Err(ParseError::InvalidOperator(format!(
                            "Expected a comparison operator (e.g. >=), not '{}'",
                            word.to_owned())))
                    }
                }
            },
            AssertionState::Threshold => {
                if let Ok(thresh) = word.parse::<f64>() {
                    threshold = Some(thresh)
                } else {
                    return Err(ParseError::InvalidThreshold(format!(
                        "Couldn't parse float from '{}'", word)))
                }
            }
        }
    }

    if threshold.is_none() {
        return Err(ParseError::InvalidThreshold(
            format!("No threshold found (e.g. '{0} N', not '{0}')",
                    operator.unwrap_or(">="))));
    }

    Ok(Assertion {
        operator: operator.expect("No operator found in predicate").to_owned(),
        op_is_negated: negated,
        threshold: threshold.expect("No threshold found in predicate"),
        point_assertion: point_assertion.expect("No point ratio found in predicate"),
        series_ratio: series_ratio,
        failure_status: status.expect("Needed to start with an exit status")
    })
}

#[cfg_attr(test, allow(dead_code))]
fn main() {
    let args = parse_args();
    let data = match fetch_data(
        &args.url, &args.path, args.window, args.retries, &args.graphite_error,
        args.print_url) {
        Ok(data) => data,
        Err(e) => {
            println!("{}", e);
            args.graphite_error.exit();
        }
    };

    let filtered = filter_to_with_data(&args.path, data.result, args.no_data);
    let with_data = match filtered {
        Ok(data) => data,
        Err(status) => {
            println!("INFO: Full query: {}", data.url);
            status.exit();
        }
    };

    let mut status = Status::Ok;
    for assertion in args.assertions {
        status = max(status,
                     do_check(&with_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status));
    }
    status.exit();
}

#[cfg(test)]
#[allow(non_snake_case)]
mod test {
    use chrono::naive::datetime::NaiveDateTime;
    use rustc_serialize::json::Json;

    use tabin_plugins::Status;

    use super::{Assertion, GraphiteData, DataPoint, operator_string_to_func,
                graphite_result_to_vec, do_check, ParseError, NegOp,
                filter_to_with_data, parse_assertion, ASSERTION_EXAMPLES};
    use super::PointAssertion::*;

    #[test]
    fn all_examples_are_accurate() {
        for assertion in ASSERTION_EXAMPLES {
            println!("testing `{}`", assertion);
            parse_assertion(assertion).unwrap();
        }
    }

    fn json_two_sets_of_graphite_data() -> Json {
        Json::from_str(r#"
        [
            {
                "datapoints": [[null, 11110], [null, 11130]],
                "target": "test.path.no-data"
            },
            {
                "datapoints": [[1, 11150], [null, 11160], [3, 11170]],
                "target": "test.path.some-data"
            }
        ]
        "#).unwrap()
    }

    fn valid_data_from_json_two_sets() -> Vec<GraphiteData> {
        vec![GraphiteData {
            points: vec![DataPoint { val: Some(1_f64),
                                     time: dt(11150) },
                         DataPoint { val: None,
                                     time: dt(11160) },
                         DataPoint { val: Some(3_f64),
                                     time: dt(11170) }],
            target: "test.path.some-data".to_owned() }]
    }

    #[test]
    fn graphite_result_to_vec_creates_a_vec_of_GraphiteData() {
        let vec = graphite_result_to_vec(&json_two_sets_of_graphite_data());
        assert_eq!(vec.len(), 2)
    }

    fn dt(t: i64) -> NaiveDateTime { NaiveDateTime::from_timestamp(t, 0) }

    #[test]
    fn operator_string_to_func_returns_a_good_filter() {
        let invalid = operator_string_to_func("<", NegOp::Yes, 5_f64);
        assert!(invalid(6_f64))
    }

    #[test]
    fn filtered_to_with_data_returns_valid_data() {
        let result = filter_to_with_data("test.path",
                                         json_two_sets_of_graphite_data(),
                                         Status::Unknown);
        let expected = valid_data_from_json_two_sets();
        match result {
            Ok(actual) => assert_eq!(actual, expected),
            Err(_) => panic!("wha")
        }
    }

    #[test]
    fn do_check_errors_with_invalid_data() {
        let result = do_check(&valid_data_from_json_two_sets(),
                              ">",
                              NegOp::Yes,
                              2.0,
                              Ratio(0.0),
                              Status::Critical);
        if let Status::Critical = result {
             /* expected */
        } else {
            panic!("Expected an Status::Critical, got: {:?}", result)
        }
    }

    #[test]
    fn do_check_succeeds_with_valid_data() {
        let result = do_check(&valid_data_from_json_two_sets(),
                              ">",
                              NegOp::Yes,
                              0.0,
                              Ratio(1.0),
                              Status::Critical);
        if let Status::Ok = result {
            /* expected */
        } else {
            panic!("Expected an Status::Ok, got: {:?}", result)
        }
    }

    #[test]
    fn parse_assertion_requires_a_starting_status() {
        let result = parse_assertion("any point is not < 100");
        if let &Err(ref e) = &result {
            if let &ParseError::NoStatusSpecifier(_) = e {
                /* expected */
            } else {
                panic!("Unexpected result: {:?}", result)
            }
        } else {
            panic!("Unexpected success: {:?}", result)
        }
    }

    #[test]
    fn parse_assertion_finds_per_point_description() {
        let predicates = parse_assertion("critical if any point is not < 100").unwrap();

        assert_eq!(predicates.operator, "<");
        assert_eq!(predicates.threshold, 100_f64);
        assert_eq!(predicates.point_assertion, Ratio(0.0));
    }

    #[test]
    fn parse_assertion_finds_per_point_description2() {
        let predicates = parse_assertion("critical if any point is not >= 5.5").unwrap();

        assert_eq!(predicates.operator, ">=");
        assert_eq!(predicates.threshold, 5.5_f64);
        assert_eq!(predicates.point_assertion, Ratio(0.));
    }

    fn json_one_point_is_below_5_5() -> Json {
        Json::from_str(r#"
            [
                {
                    "datapoints": [[6, 60], [7, 70], [8, 80]],
                    "target": "test.path.has-data"
                },
                {
                    "datapoints": [[5, 50], [6, 60]],
                    "target": "test.path.has-data"
                }
            ]
        "#).unwrap()
    }

    #[test]
    fn parse_assertion_finds_per_point_description2_and_correctly_alerts() {
        let assertion = parse_assertion("critical if any point is not >= 5.5").unwrap();
        let graphite_data = graphite_result_to_vec(&json_one_point_is_below_5_5());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        if let Status::Critical = result  {
             /* expected */
        } else {
            panic!("Expected Critical status, not '{:?}'", result)
        }
    }

    #[test]
    fn parse_series() {
        let assertion = parse_assertion("critical if any point in any series is not >= 5.5")
            .unwrap();
        assert_eq!(assertion.point_assertion, Ratio(0.0));
        assert_eq!(assertion.series_ratio, 0.0);
    }

    #[test]
    fn parse_some_series() {
        let assertion = parse_assertion(
            "critical if any point in at least 20% of series is not >= 5.5")
            .unwrap();
        assert_eq!(assertion.point_assertion, Ratio(0.0));
        assert_eq!(assertion.series_ratio, 0.2_f64);
    }

    fn json_all_points_above_5() -> Json {
        Json::from_str(r#"
            [
                {
                    "datapoints": [[6, 60], [7, 70], [8, 80], [9, 90]],
                    "target": "test.path.has-data"
                }
            ]
        "#).unwrap()
    }

    #[test]
    fn parse_all_points_and_critical() {
        let assertion = parse_assertion(
            "critical if all points are > 5")
            .unwrap();
        assert_eq!(assertion.point_assertion, Ratio(1.0));

        let graphite_data = graphite_result_to_vec(&json_all_points_above_5());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Critical);
    }

    #[test]
    fn parse_all_points_and_ok() {
        let assertion = parse_assertion(
            "critical if all points are > 5")
            .unwrap();
        assert_eq!(assertion.point_assertion, Ratio(1.0));

        let graphite_data = graphite_result_to_vec(&json_80p_of_points_are_below_6());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Ok);
    }

    #[test]
    fn parse_most_recent_point() {
        let assertion = parse_assertion(
            "critical if most recent point is > 5"
            ).unwrap();
        assert_eq!(assertion,
                   Assertion {
                       operator: ">".into(),
                       op_is_negated: NegOp::No,
                       threshold: 5.0,
                       point_assertion: Recent(1),
                       series_ratio: 0.0,
                       failure_status: Status::Critical
                   })
    }

    fn json_last_point_is_5() -> Json {
        Json::from_str(r#"
            [
                {
                    "datapoints": [[2, 20], [3, 30], [4, 40], [5, 50]],
                    "target": "test.path.has-data"
                }
            ]
        "#).unwrap()
    }

    fn json_last_existing_point_is_5() -> Json {
        Json::from_str(r#"
            [
                {
                    "datapoints": [[4, 40], [5, 50], [null, 60], [null, 70]],
                    "target": "test.path.has-data"
                }
            ]
        "#).unwrap()
    }

    #[test]
    fn most_recent_is_non_empty_works() {
        let assertion = parse_assertion("critical if most recent point is > 5").unwrap();
        let graphite_data = graphite_result_to_vec(&json_last_point_is_5());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Ok);

        let assertion = parse_assertion("critical if most recent point is > 4").unwrap();
        let graphite_data = graphite_result_to_vec(&json_last_point_is_5());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Critical);
    }

    #[test]
    fn most_recent_is_empty_works() {
        let assertion = parse_assertion("critical if most recent point is > 5").unwrap();
        let graphite_data = graphite_result_to_vec(&json_last_existing_point_is_5());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Ok);

        let assertion = parse_assertion("critical if most recent point is > 4").unwrap();
        let graphite_data = graphite_result_to_vec(&json_last_existing_point_is_5());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Critical);
    }

    #[test]
    fn most_recent_finds_okay_values_after_invalid() {
        let assertion = parse_assertion("critical if most recent point is == 4").unwrap();
        let graphite_data = graphite_result_to_vec(&json_last_point_is_5());

        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Ok);
    }

    fn json_80p_of_points_are_below_6() -> Json {
        Json::from_str(r#"
            [
                {
                    "datapoints": [[2, 20], [3, 30], [4, 40], [5, 50], [6, 60]],
                    "target": "test.path.has-data"
                }
            ]
        "#).unwrap()
    }

    #[test]
    fn parse_some_series_and_correctly_alerts() {
        let assertion = parse_assertion(
            "critical if at least 80% of of points are not >= 5.5")
            .unwrap();
        let graphite_data = graphite_result_to_vec(&json_80p_of_points_are_below_6());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Critical);
    }

    #[test]
    fn parse_some_series_positive_assertion_and_correctly_allows() {
        let assertion = parse_assertion(
            "critical if at least 79% of of points are < 6")
            .unwrap();
        let graphite_data = graphite_result_to_vec(&json_80p_of_points_are_below_6());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Critical);
    }

    #[test]
    fn parse_some_series_positive_assertion__and_correctly_allows_all_points() {
        let assertion = parse_assertion(
            "critical if at least 79% of of points are < 6")
            .unwrap();
        let graphite_data = graphite_result_to_vec(&json_80p_of_points_are_below_6());
        let result = do_check(&graphite_data,
                              &assertion.operator,
                              assertion.op_is_negated,
                              assertion.threshold,
                              assertion.point_assertion,
                              assertion.failure_status);
        assert_eq!(result, Status::Critical);
    }

    #[test]
    fn parse_some_points() {
        let assertion = parse_assertion(
            "critical if at least 20% of points are not >= 5.5")
            .unwrap();
        assert_eq!(assertion.point_assertion, Ratio(0.2));
        assert_eq!(assertion.series_ratio, 0.0);
    }

    #[test]
    fn parse_some_points_and_some_series() {
        let assertion = parse_assertion(
            "critical if at least 80% of points in at least 90% of series are not >= 5.5")
            .unwrap();
        assert_eq!(assertion.point_assertion, Ratio(0.8));
        assert_eq!(assertion.series_ratio, 0.9_f64);
    }
}