summaryrefslogtreecommitdiffstats
path: root/web/attachments/427640-nagios-plugins-openvz-beans.diff
blob: 47a8f40d3a6fec4c33df00a1c5ec5ee5b5cf959e (plain)
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
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
diff -urN nothing/check_openvz_beans.py nagios-plugins-openvz-beans/check_openvz_beans.py
--- nothing/check_openvz_beans.py	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/check_openvz_beans.py	2011-11-01 19:04:11.133266362 +0100
@@ -0,0 +1,589 @@
+#!/usr/bin/python
+#
+# Nagios check for OpenVZ bean counters
+#
+# usage:
+#   check_openvz_beans.py [options]
+#
+# see "check_openvz_beans.py -h" for detailed options
+#
+# Licensed under GPLv2 (or later)
+#
+# Copyright (C) 2011 NOKIA
+# Written by Andreas Kotes <andreas.kotes@nokia.com>
+#
+# Reviewed-by: Pascal Hahn <pascal.hahn@nokia.com>
+# Signed-Off-by: Pascal Hahn <pascal.hahn@nokia.com>
+# Signed-Off-by: Jessvin Thomas <jessvin.thomas@nokia.com>
+#
+
+import os
+import re
+import errno
+import sys
+import socket
+
+# using 'optparse' for Python 2.4 compatibility
+import optparse
+# using subprocess for clean sudo handling
+import subprocess
+
+__author__ = ('Andreas Kotes <andreas.kotes@nokia.com>')
+
+OPENVZ_CONFPATH = '/etc/vz/conf'
+OPENVZ_UBCSTORE = '/tmp/vzubc.store'
+
+class Error(Exception):
+   """Module level error."""
+   pass
+
+
+class NoBeanCountersFoundError(Error):
+   "No BeanCounters could be found in output."""
+   pass
+
+
+class BeanCounter(object):
+  """Object storing OpenVZ beancounter information"""
+  FIND_BEAN_COUNTER_RE = r'''(?xms)       # verbose, multiline, special
+    ^                                     # begin entry
+    \s*(?P<resource>\S+)                  # resource name
+    \s*(?P<held>\d+)                      # held count
+    \s*(?P<maxheld>\d+)                   # maximum held
+    \s*(?P<barrier>\d+)                   # barrier
+    \s*(?P<limit>\d+)                     # limit
+    \s*(?P<failcnt>\d+)                   # failure counter
+    '''
+  def __init__(self, uid, resource_txt, use_absolute_failcnt=False, storepath=OPENVZ_UBCSTORE):
+    """Create object storing OpenVZ beancounter information
+
+    Args
+      uid:          int  - OpenVZ context identifier
+      resource_txt: str  - OpenVZ resource
+      use_absolute_failcnt:
+                    bool - Whether to use delta failcnts or not
+      storepath:    str  - Path to OpenVZ's vzubc's data storage
+    """
+    self.uid = uid
+
+    # extract data from text line
+    resourcedata = re.match(BeanCounter.FIND_BEAN_COUNTER_RE, resource_txt).groupdict()
+    self.resource = resourcedata['resource']
+    self.held     = int(resourcedata['held'])
+    self.maxheld  = int(resourcedata['maxheld'])
+    self.barrier  = int(resourcedata['barrier'])
+    self.limit    = int(resourcedata['limit'])
+    self.failcnt  = int(resourcedata['failcnt'])
+
+    if not use_absolute_failcnt:
+       prev_failcnt = self.read_vzubc_failcnt(storepath)
+       # don't update from stale / lower value
+       if (prev_failcnt <= self.failcnt):
+           self.failcnt -= prev_failcnt
+
+  def read_vzubc_failcnt(self, storepath=OPENVZ_UBCSTORE):
+    """Read data stored by vzubc -r.
+
+    Args
+      uid:          int  - OpenVZ context identifier
+      storepath:    str  - Path to OpenVZ's vzubc's data storage
+
+    Returns:
+      int - stored value or 0
+    """
+    filename = os.path.join(storepath, "ubc.%d.%s.failcnt" % (self.uid, self.resource))
+    if os.path.exists(filename):
+        return int(open(filename, "r").read())
+    else:
+        return 0
+
+  def __repr__(self):
+    return ('<BeanCounter for uid %d resource %s: held %d maxheld %d barrier %d limit %d failcnt %d>' %
+           (self.uid, self.resource, self.held, self.maxheld, self.barrier, self.limit, self.failcnt))
+
+  def __eq__(self, other):
+    if (self.uid == other.uid and
+        self.resource == other.resource and
+        self.held == other.held and
+        self.maxheld == other.maxheld and
+        self.barrier == other.barrier and
+        self.limit == other.limit and
+        self.failcnt == other.failcnt):
+       return True
+    else:
+       return False
+
+
+class BeanCounterParser(object):
+  """Parse bean counter information."""
+  FIND_BEAN_COUNTER_GROUP_RE = r'''(?xms) # verbose, multline, special
+    ^                                     # begin entry
+    \s*(?P<uid>\d+):                      # group / context number
+    (?P<beancounters>[^:]*)$              # everything up to next group/EOF
+    '''
+
+  def parse_beancounters(self, beancounters_data, use_absolute_failcnt=False, storepath=OPENVZ_UBCSTORE):
+    """Populate BeanCounter objects with data.
+
+    Args:
+      beancounters_data: str - containing the unparsed
+      use_absolute_failcnt:
+                    bool - Whether to use delta failcnts or not
+      storepath:    str  - Path to OpenVZ's vzubc's data storage
+
+    Raises:
+      NoBeanCountersFoundError - in case no counters where found
+    """
+    # find all beans blocks
+    result = re.findall(BeanCounterParser.FIND_BEAN_COUNTER_GROUP_RE, beancounters_data)
+    if not result:
+      raise NoBeanCountersFoundError
+    self.beancounters = []
+    for uid, resource_txt in result:
+      for line in resource_txt.split("\n"):
+        if line:
+          self.beancounters.append(BeanCounter(int(uid), line, use_absolute_failcnt, storepath))
+    return self.beancounters
+
+  def __init__(self, beancounters_data, use_absolute_failcnt=False, storepath=OPENVZ_UBCSTORE):
+    """Create a new BeanCounter object
+
+    Args:
+      beancounters_data: str - containing the unparsed
+                    contents of /proc/user_beancounters
+      use_absolute_failcnt:
+                    bool - Whether to use delta failcnts or not
+      storepath:    str  - Path to OpenVZ's vzubc's data storage
+    """
+    self.parse_beancounters(beancounters_data, use_absolute_failcnt, storepath)
+
+  def get_beancounters(self):
+    return self.beancounters
+
+
+class BeanCounterThreshold(object):
+  """Holds threshold values for BeanCounters"""
+  THRESHOLD_KEYS = {
+    # relevant threshold limiting in OpenVZ
+    # see http://wiki.openvz.org/UBC_parameter_properties
+    # NOTE: custom/missing beancounters are treated limited by limit
+    'limit'  : ['lockedpages', 'numfile' , 'numiptent', 'numothersock', 'numproc', 'numpty', 'numsiginfo', 'numtcpsock', 'shmpages'],
+    'barrier': ['dcachesize', 'dgramrcvbuf', 'kmemsize', 'numflock', 'oomguarpages', 'othersockbuf', 'privvmpages', 'tcprcvbuf', 'tcpsndbuf', 'vmguarpages'],
+    'none'   : ['physpages']
+  }
+
+  def __init__(self, resource=None, critical=95, warning=90):
+    """Create a new verifier.
+
+    Args:
+      resource: str - resource name (None = wildcard match all resources)
+      critical: int - critical level (percentage of limit)
+       warning: int - critical level (percentage of limit)
+    """
+    self.resource = resource
+    self.critical = critical
+    self.warning  = warning
+
+  def __repr__(self):
+    if not self.resource:
+      resource = '*'
+    else:
+      resource = self.resource
+    return ('<BeanCounterThreshold for resource %s: critical %d warning %d>' %
+           (resource, self.critical, self.warning))
+
+  def check(self, beancounter, use_maxheld=False):
+    """Check a threshold
+
+    Args:
+      beancounter: BeanCounter object
+      use_maxheld: bool - use maxheld instead of held counter for limits
+
+    Returns:
+      None or BeanCounterViolation (or subclass)
+    """
+    if not self.resource or beancounter.resource == self.resource:
+      if beancounter.failcnt:
+        return BeanCounterFailure(beancounter.resource, beancounter.uid, beancounter.failcnt)
+
+      # what are we measuring against?
+      if beancounter.resource in BeanCounterThreshold.THRESHOLD_KEYS['barrier']:
+        quota = beancounter.barrier
+      else:
+        quota = beancounter.limit
+
+      # what are we measuring?
+      if use_maxheld:
+        value = beancounter.maxheld
+      else:
+        value = beancounter.held
+
+      if value > quota:
+        return BeanCounterOvershoot(beancounter.resource, beancounter.uid, value - quota)
+
+      # check for critical / warning by comparing our value with the relevant
+      # percentage of our quota. return an object with information how much
+      # over quota we are.
+      if quota and self.critical >= 0 and value >= (quota * (float(self.critical)/100)):
+        return BeanCounterCritical(beancounter.resource, beancounter.uid, value - int((quota * (float(self.critical)/100))))
+      if quota and self.warning >= 0 and value >= (quota * (float(self.warning)/100)):
+        return BeanCounterWarning(beancounter.resource, beancounter.uid, value - int((quota * (float(self.warning)/100))))
+    return None
+
+
+class BeanCounterViolation(object):
+  """Alert containing a BeanCounter violation."""
+  def __init__(self, resource, uid, excess):
+    """Create a new BeanCounter violation.
+
+    Args:
+      resource: str - resource name
+           uid: int - context uid
+        excess: int - how much the limit was exceeded
+    """
+    self.tag = 'VIOL'
+    self.resource = resource
+    self.uid = uid
+    self.excess = excess
+
+  def __repr__(self):
+    return ('<%s %s: resource %s in context %d by %d>' %
+           (self.__class__.__name__, self.tag, self.resource, self.uid, self.excess))
+
+
+class BeanCounterFailure(BeanCounterViolation):
+  """Alert containing a BeanCounter failure."""
+  def __init__(self, resource, uid, excess):
+    """Create a new BeanCounter failure.
+
+    Args:
+      resource: str - resource name
+           uid: int - context uid
+        excess: int - how much the limit was exceeded
+    """
+    super(BeanCounterFailure, self).__init__(resource, uid, excess)
+    self.tag = 'FAIL'
+
+
+class BeanCounterOvershoot(BeanCounterViolation):
+  """Alert containing a BeanCounter overshoot."""
+  def __init__(self, resource, uid, excess):
+    """Create a new BeanCounter overshoot.
+
+    Args:
+      resource: str - resource name
+           uid: int - context uid
+        excess: int - how much the limit was exceeded
+    """
+    super(BeanCounterOvershoot, self).__init__(resource, uid, excess)
+    self.tag = 'OVER'
+
+
+class BeanCounterCritical(BeanCounterViolation):
+  """Alert containing a BeanCounter critical."""
+  def __init__(self, resource, uid, excess):
+    """Create a new BeanCounter critical alert
+
+    Args:
+      resource: str - resource name
+           uid: int - context uid
+        excess: int - how much the limit was exceeded
+    """
+    super(BeanCounterCritical, self).__init__(resource, uid, excess)
+    self.tag = 'CRIT'
+
+
+class BeanCounterWarning(BeanCounterViolation):
+  """Alert containing a BeanCounter warning."""
+  def __init__(self, resource, uid, excess):
+    """Create a new BeanCounter warning alert
+
+    Args:
+      resource: str - resource name
+           uid: int - context uid
+        excess: int - how much the limit was exceeded
+    """
+    super(BeanCounterWarning, self).__init__(resource, uid, excess)
+    self.tag = 'WARN'
+
+
+class BeanCounterVerifier(object):
+  """Verifier wrapper for bean counters."""
+  def __init__(self, options):
+    """Create a new set of verifiers
+
+    Args:
+      options: optparse.Values, options used:
+                warning:  int - generic warning level
+               critical:  int - generic critical level
+            res_warning: dict - resource name:int level
+           res_critical: dict - resource name:int level
+    """
+    # add common thresholds
+    self.thresholds = [BeanCounterThreshold(None, options.critical, options.warning)]
+    for resource, level in options.res_warning.items():
+       # has both critical and warning values?
+       if resource in options.res_critical:
+         self.thresholds.append(BeanCounterThreshold(resource, options.res_critical[resource], level))
+       else:
+         self.thresholds.append(BeanCounterThreshold(resource, -1, level))
+    for resource, level in options.res_critical.iteritems():
+       # already defined during warning value loop?
+       if not resource in options.res_warning:
+         self.thresholds.append(BeanCounterThreshold(resource, level, -1))
+
+  def verify(self, beancounters, use_maxheld=False):
+    """Verify BeanCounters
+
+    Args:
+      beancounters: list of BeanCounters
+      use_maxheld: bool - use maxheld instead of held counter for limits
+
+    Returns:
+      list of lists (see BeanCounterThreshold.check)
+    """
+    results = []
+    for beancounter in beancounters:
+      for threshold in self.thresholds:
+        result = threshold.check(beancounter, use_maxheld)
+        if result:
+          results.append(result)
+    return results
+
+
+def shorten_hostname(hostname, parts=2):
+  """Shorten hostname by taking only certain parts from the beginning of it.
+
+  Args:
+    hostname: str - FQDN
+       parts: int - number of parts to keep
+  """
+  # return at least the hostname
+  if (parts < 1):
+    parts = 1
+  if hostname:
+    return '.'.join(hostname.split('.')[:parts])
+  return None
+
+
+def get_vm_hostname(uid, confpath=OPENVZ_CONFPATH):
+  """Get hostname for a vm by getting the hostname from config file
+
+  Args:
+         uid: int - uid / container context number of vm
+    confpath: str - folder in which to find <uid>.conf
+
+  Returns:
+    str: FQDN as configured, or None
+  """
+  try:
+     result = re.search(r'HOSTNAME="(.*)"', open(confpath+"/"+str(uid)+".conf", "r").read())
+     if result:
+       return result.group(1)
+  except IOError:
+    return None
+
+
+def get_local_fqdn():
+  """Get local hostname."""
+  return socket.gethostname()
+
+
+def get_hostname(uid, confpath=OPENVZ_CONFPATH, parts=2):
+  """Get and shorten OpenVZ vm hostname
+
+  Args:
+         uid: ints - uid / container context number
+    confpath: str - folder in which to find <uid>.conf
+       parts: int - number of hostname parts to return
+
+  Returns:
+    str: hostname
+  """
+  # the hypervisor itself, or a VM -> use local hostname
+  # NOTE: this assumes a correct confpath and non-existence of it on VMs.
+  if uid == 0 or not os.path.exists(confpath):
+    hostname = get_local_fqdn()
+  else:
+    hostname = get_vm_hostname(uid, confpath)
+  # drop ending domain parts of FQDN
+  return shorten_hostname(hostname, parts)
+
+
+def read_data(options):
+  """Read OpenVZ counter data from system
+
+  Args:
+    options: optparse object:
+          sudo: bool - use sudo to read file or not
+      filename: str  - filename to read
+
+  Return:
+    str: verbatim file contents
+
+  Sideeffects:
+    May exit to system directly.
+  """
+  # read into a string directly, or with a "sudo cat"
+  if not options.sudo:
+    try:
+      data = open(options.filename, "r").read()
+    except IOError, (errorcode, errorstr):
+      # file not found? not an OpenVZ system!
+      if (errorcode == errno.ENOENT):
+        print "OK: Not an OpenVZ system, no need to worry about beancounters."
+        sys.exit(0)
+      elif (errorcode == errno.EACCES):
+        print "UNKNOWN: Not permitted to read beancounter information."
+        sys.exit(3)
+      else:
+        print "CRITICAL: Unknown problem reading beancounters."
+        sys.exit(2)
+  else:
+    # call a non-interactive sudo to read the file
+    # needs to be configured via e.g. /etc/sudoers
+    try:
+      data = subprocess.Popen(["/usr/bin/sudo", "-n", "cat", options.filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
+    except OSError, (errorcode, errorstr):
+      print "CRITICAL: Can't execute sudo to read beancounters. (%d - %s)" % (errorcode, errorstr)
+      sys.exit(2)
+    if not data:
+      print "CRITICAL: Trying to read beancounters with sudo didn't yield data."
+      sys.exit(2)
+  return data
+
+
+def create_optionsparser():
+  """Create optparse.OptionParser object for us
+
+  Returns:
+    optparse.OptionParser object
+  """
+  usage = "usage: %prog [options]"
+  parser = optparse.OptionParser(usage=usage, option_class=ExtOption)
+  parser.add_option("-w", "--warning",       dest="warning",      default=90,
+    action="store_int",   help="default warning threshold percentage for everything (default: %default)")
+  parser.add_option("-c", "--critical",      dest="critical",     default=95,
+    action="store_int",   help="default critical threshold percentage for everything (default: %default)")
+  parser.add_option("-W", "--resource-warning",    dest="res_warning",  default={},
+    action="keyvalgroup", help="resource specific warning percentage (e.g. foo:50,bar:60)")
+  parser.add_option("-C", "--resource-critical",  dest="res_critical", default={},
+    action="keyvalgroup", help="resource-specific critical percentage (e.g. foo:90,bar:95)")
+  parser.add_option("-f", "--file", dest="filename", default='/proc/user_beancounters',
+    action="store",       help="read beancounters from file (default: %default)")
+  parser.add_option("-s", "--sudo", dest="sudo", default=False,
+    action="store_true",  help="use 'sudo cat filename' to read file as root (filename see -f)")
+  parser.add_option("-p", "--configs", dest="confpath", default=OPENVZ_CONFPATH,
+    action="store",       help="path for OpenVZ config path (to get hostnames, default: %default)")
+  parser.add_option("-d", "--domainparts", dest="domainparts", default=2,
+    action="store_int",   help="how many domain parts of the hostname to keep (default: %default)")
+  parser.add_option("-u", "--ubc-store-path", dest="storepath", default=OPENVZ_UBCSTORE,
+    action="store",       help="path for vzubc relative information (default: %default)")
+  parser.add_option("-m", "--use-maxheld", dest="use_maxheld", default=False,
+    action="store_true",  help="use resource's maxheld instead of current_held (default: %default)")
+  parser.add_option("-a", "--absolute-fails", dest="use_absolute_failcnt", default=False,
+    action="store_true",  help="don't use vzubc's relative fail counters (default: %default)")
+  return parser
+
+
+class ExtOption(optparse.Option):
+  """Specialized Option class which parses key:val,key:val parameters and integers
+  See optparse documentation for detailed explanation of how this works."""
+  ACTIONS = optparse.Option.ACTIONS + ("keyvalgroup", "store_int",)
+  STORE_ACTIONS = optparse.Option.STORE_ACTIONS + ("keyvalgroup", "store_int",)
+  TYPED_ACTIONS = optparse.Option.TYPED_ACTIONS + ("keyvalgroup", "store_int",)
+  ALWAYS_TYPED_ACTIONS = optparse.Option.ALWAYS_TYPED_ACTIONS + ("keyvalgroup", "store_int",)
+
+  def take_action(self, action, dest, opt, value, values, parser):
+    if action == "keyvalgroup":
+      keyvals = value.split(",")
+      for keyval in keyvals:
+        key, val = keyval.split(":")
+        if re.match(r'\d+', val):
+          val = int(val)
+        values.ensure_value(dest, {}).update({key:val})
+    elif action == "store_int":
+      setattr(values, dest, int(value))
+    else:
+      optparse.Option.take_action(self, action, dest, opt, value, values, parser)
+
+
+def __main__():
+  optionsparser = create_optionsparser()
+  # args parsing will exit the program if options are used wrong or help is
+  # requested. optionsparser.error exists to system as well - we call it when
+  # extra arguments are given. none are expected, we use options only.
+  options, args = optionsparser.parse_args()
+  if args:
+     optionsparser.error("incorrect number of arguments")
+
+  # NOTE: read_data itself may exit to system in some cases
+  data = read_data(options)
+  if not data:
+    print "CRITICAL: No data given while reading beancounters."
+    sys.exit(2)
+
+  # parse beancounters, create verifiers, verify beancounters
+  try:
+    beancounterparser   = BeanCounterParser(data, options.use_absolute_failcnt, options.storepath)
+  except NoBeanCountersFoundError:
+    print "CRITICAL: No beancounters found in data read."
+    sys.exit(2)
+
+  beancounterverifier = BeanCounterVerifier(options)
+  beancounteralerts   = beancounterverifier.verify(beancounterparser.get_beancounters(), options.use_maxheld)
+
+  # find uids and combine alert groups
+  fail = {}
+  over = {}
+  crit = {}
+  warn = {}
+  for alert in beancounteralerts:
+    if isinstance (alert, BeanCounterFailure):
+      fail.setdefault(alert.uid, {})
+      fail[alert.uid][alert.resource] = alert.excess
+    elif isinstance (alert, BeanCounterOvershoot):
+      over.setdefault(alert.uid, {})
+      over[alert.uid][alert.resource] = alert.excess
+    elif isinstance (alert, BeanCounterCritical):
+      crit.setdefault(alert.uid, {})
+      crit[alert.uid][alert.resource] = alert.excess
+    elif isinstance (alert, BeanCounterWarning):
+      warn.setdefault(alert.uid, {})
+      warn[alert.uid][alert.resource] = alert.excess
+
+  # default message & exitcode if everything is fine
+  message  = "OK: all beancounters below configured thresholds"
+  perfdata = ""
+  exitcode = 0
+
+  # set message prefix and errocode accordingly
+  if fail or over or crit:
+    message  = "CRITICAL:"
+    exitcode = 2
+  elif warn:
+    message  = "WARNING:"
+    exitcode = 1
+
+  # combine specific Nagios message(s)
+  for level, tag in (fail, 'FAIL'), (over, 'OVER'), (crit, 'CRIT'), (warn, 'WARN'):
+    if level:
+      message  += " %s:" % tag
+      perfdata += " %s:" % tag
+      for uid, counters in level.items():
+        hostname = get_hostname(uid, options.confpath, options.domainparts)
+        message  += " %s(%d)" % (hostname, uid)
+        perfdata += " HOST %s(%d):" % (hostname, uid)
+        for resource, counter in counters.items():
+          perfdata += " %s(+%d)" % (resource, counter)
+
+  # output message & exit
+  if len(perfdata):
+    print "%s|%s" % (message, perfdata)
+  else:
+    print message
+  sys.exit(exitcode)
+
+
+if __name__ == "__main__":
+  __main__()
Binary files nothing/check_openvz_beans.pyc and nagios-plugins-openvz-beans/check_openvz_beans.pyc differ
diff -urN nothing/check_openvz_beans-test.py nagios-plugins-openvz-beans/check_openvz_beans-test.py
--- nothing/check_openvz_beans-test.py	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/check_openvz_beans-test.py	2011-11-01 18:28:47.867345265 +0100
@@ -0,0 +1,775 @@
+#!/usr/bin/python
+"""check_openvz_beans unit tests."""
+
+__author__ = 'Andreas Kotes <andreas.kotes@nokia.com>'
+
+import __builtin__
+import os
+import sys
+import unittest
+import mox
+import optparse
+import StringIO
+import subprocess
+import check_openvz_beans
+
+class BeanCounterTest(mox.MoxTestBase):
+  """Test the BeanCounter class."""
+  def testCreate(self):
+    beancounter = check_openvz_beans.BeanCounter(123, "dummy 1 2 3 4 5")
+    self.assertEquals(beancounter.uid,      123)
+    self.assertEquals(beancounter.resource, 'dummy')
+    self.assertEquals(beancounter.held,     1)
+    self.assertEquals(beancounter.maxheld,  2)
+    self.assertEquals(beancounter.barrier,  3)
+    self.assertEquals(beancounter.limit,    4)
+    self.assertEquals(beancounter.failcnt,  5)
+    self.assertEquals(beancounter.__repr__(),
+      '<BeanCounter for uid 123 resource dummy: held 1 maxheld 2 barrier 3 limit 4 failcnt 5>')
+    self.assertTrue(beancounter == check_openvz_beans.BeanCounter(123, "dummy 1 2 3 4 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(124, "dummy 1 2 3 4 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(123, "yummy 1 2 3 4 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(123, "dummy 2 2 3 4 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(123, "dummy 1 3 3 4 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(123, "dummy 1 2 4 4 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(123, "dummy 1 2 3 5 5"))
+    self.assertFalse(beancounter == check_openvz_beans.BeanCounter(123, "dummy 1 2 3 4 6"))
+
+
+class BeanCounterParserTest(mox.MoxTestBase):
+  """Test the BeanCounterParser class."""
+  def setUp(self):
+    super(BeanCounterParserTest, self).setUp()
+
+  def test_init_and_parsebeancounters(self):
+    beancounters = check_openvz_beans.BeanCounterParser("123: dummy1 1 2 3 4 5\n dummy2 54 63 62 13 53\n234: dummy3 9 8 7 6 5\n")
+    self.assertEquals(beancounters.get_beancounters(), [
+     check_openvz_beans.BeanCounter(123, ' dummy1 1 2 3 4 5'),
+     check_openvz_beans.BeanCounter(123, ' dummy2 54 63 62 13 53'),
+     check_openvz_beans.BeanCounter(234, ' dummy3 9 8 7 6 5')])
+
+  def test_parsefail(self):
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(check_openvz_beans.NoBeanCountersFoundError, check_openvz_beans.BeanCounterParser, "nonsense")
+
+
+class BeanCounterThresholdTest(mox.MoxTestBase):
+  """Test the BeanCounterThreshold class."""
+  def setUp(self):
+    super(BeanCounterThresholdTest, self).setUp()
+    # Mock BeanCounters
+    self.beancounter_mock = self.mox.CreateMock(check_openvz_beans.BeanCounter)
+    self.beancounter_mock.uid      = 123
+    self.beancounter_mock.failcnt  = 0
+
+  def testCreateSimple(self):
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    self.assertEquals(threshold.resource, None)
+    self.assertEquals(threshold.critical, 95)
+    self.assertEquals(threshold.warning,  90)
+    self.assertEquals(threshold.__repr__(),
+      '<BeanCounterThreshold for resource *: critical 95 warning 90>')
+
+  def testCreateCustom(self):
+    threshold = check_openvz_beans.BeanCounterThreshold('dummy', 70, 60)
+    self.assertEquals(threshold.resource, 'dummy')
+    self.assertEquals(threshold.critical, 70)
+    self.assertEquals(threshold.warning,  60)
+    self.assertEquals(threshold.__repr__(),
+      '<BeanCounterThreshold for resource dummy: critical 70 warning 60>')
+
+  def testCheckSimpleOkay(self):
+    self.beancounter_mock.resource = 'dummy_okay'
+    self.beancounter_mock.held     = 1
+    self.beancounter_mock.maxheld  = 2
+    self.beancounter_mock.barrier  = 3
+    self.beancounter_mock.limit    = 4
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    self.assertFalse(threshold.check(self.beancounter_mock))
+
+  def testCheckSimpleOkayNegWarn(self):
+    self.beancounter_mock.resource = 'dummy_okay'
+    self.beancounter_mock.held     = 1
+    self.beancounter_mock.maxheld  = 2
+    self.beancounter_mock.barrier  = 3
+    self.beancounter_mock.limit    = 4
+    threshold = check_openvz_beans.BeanCounterThreshold(warning=-1)
+    self.assertEqual(threshold.check(self.beancounter_mock), None)
+
+  def testCheckSimpleOkayNegCrit(self):
+    self.beancounter_mock.resource = 'dummy_okay'
+    self.beancounter_mock.held     = 1
+    self.beancounter_mock.maxheld  = 2
+    self.beancounter_mock.barrier  = 3
+    self.beancounter_mock.limit    = 4
+    threshold = check_openvz_beans.BeanCounterThreshold(critical=-1)
+    self.assertEqual(threshold.check(self.beancounter_mock), None)
+
+  def testCheckSimpleFail(self):
+    # failcnt engaged
+    self.beancounter_mock.resource = 'dummy_fail'
+    self.beancounter_mock.held     = 1
+    self.beancounter_mock.maxheld  = 2
+    self.beancounter_mock.barrier  = 3
+    self.beancounter_mock.limit    = 4
+    self.beancounter_mock.failcnt  = 1
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'FAIL')
+
+  def testCheckSimpleEqual(self):
+    # eq Limit
+    self.beancounter_mock.resource = 'dummy_equal'
+    self.beancounter_mock.held     = 50
+    self.beancounter_mock.maxheld  = 100
+    self.beancounter_mock.barrier  = 100
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold(critical=101, warning=101)
+    self.assertFalse(threshold.check(self.beancounter_mock))
+
+  def testCheckSimpleOverGt(self):
+    # over Limit
+    self.beancounter_mock.resource = 'dummy_over'
+    self.beancounter_mock.held     = 4
+    self.beancounter_mock.maxheld  = 4
+    self.beancounter_mock.barrier  = 3
+    self.beancounter_mock.limit    = 3
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'OVER')
+
+  def testCheckSimpleCritEq(self):
+    # eq critical value
+    self.beancounter_mock.resource = 'dummy_crit_eq'
+    self.beancounter_mock.held     = 95
+    self.beancounter_mock.maxheld  = 95
+    self.beancounter_mock.barrier  = 100
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'CRIT')
+
+  def testCheckSimpleCritGt(self):
+    # gt critical value
+    self.beancounter_mock.resource = 'dummy_crit_gt'
+    self.beancounter_mock.held     = 96
+    self.beancounter_mock.maxheld  = 96
+    self.beancounter_mock.barrier  = 100
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'CRIT')
+
+  def testCheckSimpleWarnEq(self):
+    # eq warning value
+    self.beancounter_mock.resource = 'dummy_warn_eq'
+    self.beancounter_mock.held     = 90
+    self.beancounter_mock.maxheld  = 90
+    self.beancounter_mock.barrier  = 100
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'WARN')
+
+  def testCheckSimpleWarnGt(self):
+    # gt warning value
+    self.beancounter_mock.resource = 'dummy_warn_gt'
+    self.beancounter_mock.held     = 91
+    self.beancounter_mock.maxheld  = 91
+    self.beancounter_mock.barrier  = 100
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'WARN')
+
+  def testCheckBarrierQuota(self):
+    # limited by barrier -> would not warn if limited by limit
+    self.beancounter_mock.resource = 'numflock'
+    self.beancounter_mock.held     = 45
+    self.beancounter_mock.maxheld  = 45
+    self.beancounter_mock.barrier  = 50
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    result = threshold.check(self.beancounter_mock)
+    self.assertTrue(result)
+    self.assertEquals(result.tag, 'WARN')
+
+  def testCheckLimitQuota(self):
+    # limited by limit -> would be over if limited by barrier
+    self.beancounter_mock.resource = 'numproc'
+    self.beancounter_mock.held     = 50
+    self.beancounter_mock.maxheld  = 56
+    self.beancounter_mock.barrier  = 50
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold()
+    self.assertFalse(threshold.check(self.beancounter_mock))
+
+  def testCheckNotResponsible(self):
+    # gt critical value
+    self.beancounter_mock.resource = 'dummy_crit_gt'
+    self.beancounter_mock.held     = 50
+    self.beancounter_mock.maxheld  = 96
+    self.beancounter_mock.barrier  = 100
+    self.beancounter_mock.limit    = 100
+    threshold = check_openvz_beans.BeanCounterThreshold('differentresource')
+    self.assertFalse(threshold.check(self.beancounter_mock))
+
+
+class BeanCounterViolationTest(unittest.TestCase):
+  """Test the BeanCounterViolation class."""
+  def testCreate(self):
+    violation = check_openvz_beans.BeanCounterViolation('dummy', 123, 10)
+    self.assertEquals(violation.uid,      123)
+    self.assertEquals(violation.resource, 'dummy')
+    self.assertEquals(violation.tag,      'VIOL')
+    self.assertEquals(violation.excess,   10)
+    self.assertEquals(violation.__repr__(),
+      '<BeanCounterViolation VIOL: resource dummy in context 123 by 10>')
+
+
+class BeanCounterFailureTest(unittest.TestCase):
+  """Test the BeanCounterFailure class."""
+  def testCreate(self):
+    violation = check_openvz_beans.BeanCounterFailure('dummy', 123, 10)
+    self.assertEquals(violation.uid,      123)
+    self.assertEquals(violation.resource, 'dummy')
+    self.assertEquals(violation.tag,      'FAIL')
+    self.assertEquals(violation.excess,   10)
+    self.assertEquals(violation.__repr__(),
+      '<BeanCounterFailure FAIL: resource dummy in context 123 by 10>')
+
+
+class BeanCounterOvershootTest(unittest.TestCase):
+  """Test the BeanCounterOvershoot class."""
+  def testCreate(self):
+    violation = check_openvz_beans.BeanCounterOvershoot('dummy', 123, 10)
+    self.assertEquals(violation.uid,      123)
+    self.assertEquals(violation.resource, 'dummy')
+    self.assertEquals(violation.tag,      'OVER')
+    self.assertEquals(violation.excess,   10)
+    self.assertEquals(violation.__repr__(),
+      '<BeanCounterOvershoot OVER: resource dummy in context 123 by 10>')
+
+
+class BeanCounterCritical(unittest.TestCase):
+  """Test the BeanCounterCritical class."""
+  def testCreate(self):
+    violation = check_openvz_beans.BeanCounterCritical('dummy', 123, 10)
+    self.assertEquals(violation.uid,      123)
+    self.assertEquals(violation.resource, 'dummy')
+    self.assertEquals(violation.tag,      'CRIT')
+    self.assertEquals(violation.excess,   10)
+    self.assertEquals(violation.__repr__(),
+      '<BeanCounterCritical CRIT: resource dummy in context 123 by 10>')
+
+
+class BeanCounterWarningTest(unittest.TestCase):
+  """Test the BeanCounterWarning class."""
+  def testCreate(self):
+    violation = check_openvz_beans.BeanCounterWarning('dummy', 123, 10)
+    self.assertEquals(violation.uid,      123)
+    self.assertEquals(violation.resource, 'dummy')
+    self.assertEquals(violation.tag,      'WARN')
+    self.assertEquals(violation.excess,   10)
+    self.assertEquals(violation.__repr__(),
+      '<BeanCounterWarning WARN: resource dummy in context 123 by 10>')
+
+
+class BeanCounterVerifierInitTest(mox.MoxTestBase):
+  """Test the BeanCounterVerifier class (1/2)."""
+  def setUp(self):
+    super(BeanCounterVerifierInitTest, self).setUp()
+    # Mock BeanCounterThresholds
+    self.values_mock = self.mox.CreateMock(optparse.Values)
+    self.threshold_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterThreshold)
+    self.mox.StubOutWithMock(check_openvz_beans, 'BeanCounterThreshold')
+
+    # check with common thresholds
+    self.values_mock.critical = 95
+    self.values_mock.warning = 90
+    check_openvz_beans.BeanCounterThreshold(None, 95, 90)
+
+    # check with resource-specific thresholds
+    self.values_mock.res_critical = {'foo':20, 'bar':30}
+    self.values_mock.res_warning =  {'foo':10, 'baz':20}
+    check_openvz_beans.BeanCounterThreshold('foo', 20, 10)
+    check_openvz_beans.BeanCounterThreshold('baz', -1, 20)
+    check_openvz_beans.BeanCounterThreshold('bar', 30, -1)
+
+    self.mox.ReplayAll()
+
+  def test_init(self):
+    beancounterverifier = check_openvz_beans.BeanCounterVerifier(self.values_mock)
+
+
+class BeanCounterVerifierVerifyTest(mox.MoxTestBase):
+  """Test the BeanCounterVerifier class (2/2)."""
+  def setUp(self):
+    super(BeanCounterVerifierVerifyTest, self).setUp()
+    # Mock BeanCounterThresholds
+    self.values_mock = self.mox.CreateMock(optparse.Values)
+    self.threshold_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterThreshold)
+    self.beancounter_mock = self.mox.CreateMock(check_openvz_beans.BeanCounter)
+    self.violation_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterViolation)
+    self.mox.StubOutWithMock(check_openvz_beans, 'BeanCounterThreshold')
+    self.mox.StubOutWithMock(check_openvz_beans.BeanCounterThreshold, 'check')
+
+    # check with common thresholds
+    self.values_mock.critical = 95
+    self.values_mock.warning = 90
+    self.values_mock.res_critical = {}
+    self.values_mock.res_warning = {}
+    check_openvz_beans.BeanCounterThreshold(None, 95, 90).AndReturn(self.threshold_mock)
+    self.threshold_mock.check(mox.IsA(check_openvz_beans.BeanCounter), False)
+    self.threshold_mock.check(mox.IsA(check_openvz_beans.BeanCounter), False).AndReturn(self.violation_mock)
+    self.threshold_mock.check(mox.IsA(check_openvz_beans.BeanCounter), False).AndReturn(self.violation_mock)
+    self.threshold_mock.check(mox.IsA(check_openvz_beans.BeanCounter), False).AndReturn(self.violation_mock)
+
+    self.mox.ReplayAll()
+
+  def test_verify(self):
+    beancounterverifier = check_openvz_beans.BeanCounterVerifier(self.values_mock)
+    beancounter_mock = self.mox.CreateMock(check_openvz_beans.BeanCounter)
+    # first one is okay
+    self.assertEquals(len(beancounterverifier.verify([beancounter_mock,], False)), 0)
+    # second one has a violation
+    self.assertEquals(len(beancounterverifier.verify([beancounter_mock,], False)), 1)
+    # third + fourth each have a violation
+    self.assertEquals(len(beancounterverifier.verify([beancounter_mock, beancounter_mock], False)), 2)
+
+
+class ExtOptionTest(mox.MoxTestBase):
+  """Test the ExtOption class."""
+  def setUp(self):
+    super(ExtOptionTest, self).setUp()
+    # Mock optparse objects
+    self.values1 = self.mox.CreateMockAnything()
+    self.values1.ensure_value('foobar', {}).AndReturn(self.values1)
+    self.values1.update({'key1': 'val1'})
+    self.values1.ensure_value('foobar', {}).AndReturn(self.values1)
+    self.values1.update({'key2': 'val2'})
+
+    self.values2 = self.mox.CreateMockAnything()
+    self.values2.intval = 0
+
+    self.values3 = self.mox.CreateMockAnything()
+
+    self.mox.StubOutWithMock(optparse.Option, '__init__')
+    self.mox.StubOutWithMock(optparse.Option, 'take_action')
+    optparse.Option.__init__()
+    optparse.Option.take_action(mox.IsA(optparse.Option), 'something_else', None, None, None, None, None)
+
+    self.mox.ReplayAll()
+
+  def test_take_action(self):
+    option = check_openvz_beans.ExtOption()
+    option.take_action('keyvalgroup', 'foobar', 'dunno', 'key1:val1,key2:val2', self.values1, 'dunno')
+
+    self.assertEquals(self.values2.intval, 0)
+    option.take_action('store_int',   'intval', 'dunno', '10', self.values2, 'dunno')
+    self.assertEquals(self.values2.intval, 10)
+
+    option.take_action('something_else', None, None, None, None, None)
+
+
+class ReadDataTest(mox.MoxTestBase):
+  """Check read_data functions without sudo."""
+  def setUp(self):
+    super(ReadDataTest, self).setUp()
+    # Mock optparse objects
+    self.options_mock = self.mox.CreateMock(optparse.Values)
+    self.popen_mock = self.mox.CreateMock(subprocess.Popen)
+    self.mox.StubOutWithMock(subprocess, 'Popen')
+    self.saved_stdout = sys.stdout
+    sys.stdout = StringIO.StringIO()
+
+  def tearDown(self):
+    super(ReadDataTest, self).tearDown()
+    sys.stdout = self.saved_stdout
+
+  def test_read_data(self):
+    """Test reading OpenVZ counter data from system without sudo - existing file."""
+    self.options_mock.sudo = False
+    self.options_mock.filename = 'testdata/hostcrit.bcs'
+    self.mox.ReplayAll()
+
+    result = check_openvz_beans.read_data(self.options_mock)
+
+    self.assertTrue(isinstance(result, str))
+    self.assertEquals(len(result), os.path.getsize(self.options_mock.filename))
+
+  def test_read_data_missing_file(self):
+    """Test reading OpenVZ counter data from system without sudo - missing file."""
+    self.options_mock.sudo = False
+    self.options_mock.filename = '/nonexisting/foobar'
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.read_data, self.options_mock)
+    self.assertEqual(sys.stdout.getvalue(), 'OK: Not an OpenVZ system, no need to worry about beancounters.\n')
+
+  def test_read_data_missing_file(self):
+    """Test reading OpenVZ counter data from system without sudo - unreadable file."""
+    self.options_mock.sudo = False
+    self.options_mock.filename = 'testdata/unreadable.bcs'
+    try:
+      os.unlink(self.options_mock.filename)
+    except OSError:
+      pass
+
+    open(self.options_mock.filename, 'w').close()
+    os.chmod(self.options_mock.filename, 0000)
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.read_data, self.options_mock)
+    self.assertEqual(sys.stdout.getvalue(), 'UNKNOWN: Not permitted to read beancounter information.\n')
+
+    try:
+      os.unlink(self.options_mock.filename)
+    except OSError:
+      pass
+
+  # FIXME: not all code pathes completely tested due to unmockability of open()
+
+  def test_read_data_sudo(self):
+    """Test reading OpenVZ counter data from system with sudo - sucess."""
+    self.options_mock.sudo = True
+    self.options_mock.filename = 'irrelevant'
+    subprocess.Popen(['/usr/bin/sudo', '-n', 'cat', 'irrelevant'], stderr=-1, stdout=-1).AndReturn(self.popen_mock)
+    self.popen_mock.communicate().AndReturn(['somedata'])
+    self.mox.ReplayAll()
+
+    result = check_openvz_beans.read_data(self.options_mock)
+
+    self.assertTrue(isinstance(result, str))
+    self.assertEquals(result, 'somedata')
+
+  def test_read_data_sudo_oserror(self):
+    """Test reading OpenVZ counter data from system with sudo - OSError."""
+    self.options_mock.sudo = True
+    self.options_mock.filename = 'irrelevant'
+    subprocess.Popen(['/usr/bin/sudo', '-n', 'cat', 'irrelevant'], stderr=-1, stdout=-1).AndRaise(OSError(42, 'mocketimock'))
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.read_data, self.options_mock)
+    self.assertEqual(sys.stdout.getvalue(), "CRITICAL: Can't execute sudo to read beancounters. (42 - mocketimock)\n")
+
+  def test_read_data_sudo_nodata(self):
+    """Test reading OpenVZ counter data from system with sudo - no data."""
+    self.options_mock.sudo = True
+    self.options_mock.filename = 'irrelevant'
+
+    subprocess.Popen(['/usr/bin/sudo', '-n', 'cat', 'irrelevant'], stderr=-1, stdout=-1).AndReturn(self.popen_mock)
+    self.popen_mock.communicate().AndReturn([''])
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.read_data, self.options_mock)
+    self.assertEqual(sys.stdout.getvalue(), "CRITICAL: Trying to read beancounters with sudo didn't yield data.\n")
+
+
+class GlobalsTest(mox.MoxTestBase):
+  """Check global functions."""
+  def setUp(self):
+    super(GlobalsTest, self).setUp()
+    self.saved_stdout, sys.stdout = sys.stdout, StringIO.StringIO()
+
+  def tearDown(self):
+    super(GlobalsTest, self).tearDown()
+    sys.stdout = self.saved_stdout
+
+  def test_shorten_hostname(self):
+    """Test shortening hostnames."""
+    self.testfqdn = "hostname.with.long.domain.name"
+    # the (short) hostname should be returned in any case
+    self.assertEqual(check_openvz_beans.shorten_hostname(self.testfqdn,  0), "hostname")
+    self.assertEqual(check_openvz_beans.shorten_hostname(self.testfqdn,  1), "hostname")
+    # various lengths should be returned properly
+    self.assertEqual(check_openvz_beans.shorten_hostname(self.testfqdn,  2), "hostname.with")
+    self.assertEqual(check_openvz_beans.shorten_hostname(self.testfqdn,  5), "hostname.with.long.domain.name")
+    # if more is requested than is there, just give all there is
+    self.assertEqual(check_openvz_beans.shorten_hostname(self.testfqdn, 42), "hostname.with.long.domain.name")
+
+  def test_get_vm_hostname(self):
+    """Test getting vm hostnames."""
+    # two known existing hosts
+    self.assertEqual(check_openvz_beans.get_vm_hostname(10, 'testdata'), 'vmokay.pr.foo.test.your.do.main')
+    self.assertEqual(check_openvz_beans.get_vm_hostname(60, 'testdata'), 'vmover1.st.foo.test.your.do.main')
+    # one host whose config file doesn't contain a hostname
+    self.assertEqual(check_openvz_beans.get_vm_hostname(99, 'testdata'), None)
+    # one host where no config file exists
+    self.assertEqual(check_openvz_beans.get_vm_hostname(90, 'testdata'), None)
+
+  def test_get_local_fqdn(self):
+    """Test getting local hostnames."""
+    # due to the local nature of hostnames we can only check wether it has a qualifying '.' in it
+    self.assertTrue(check_openvz_beans.get_local_fqdn().find('.') > 0)
+
+  def test_get_hostname(self):
+    """Test getting and shortening hostnames."""
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_vm_hostname')
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_local_fqdn')
+    check_openvz_beans.get_local_fqdn().AndReturn('localhost.localdomain')
+    check_openvz_beans.get_local_fqdn().AndReturn('localhost.localdomain')
+    check_openvz_beans.get_vm_hostname(10, 'testdata').AndReturn('hostname.with.long.domain.name')
+    self.mox.ReplayAll()
+
+    self.assertEqual(check_openvz_beans.get_hostname(0, 'testdata'), 'localhost.localdomain')
+    self.assertEqual(check_openvz_beans.get_hostname(99, 'nonexistent'), 'localhost.localdomain')
+    self.assertEqual(check_openvz_beans.get_hostname(10, 'testdata'), 'hostname.with')
+
+    self.mox.VerifyAll()
+    self.mox.UnsetStubs()
+
+  def test_create_optionsparser(self):
+    """Test creating a custom OptionParser."""
+    parser = check_openvz_beans.create_optionsparser()
+    self.assertTrue(isinstance(parser, optparse.OptionParser))
+    # FIXME: do we want to check more?
+
+
+class MainEarlyFailuresTest(mox.MoxTestBase):
+  def setUp(self):
+    super(MainEarlyFailuresTest, self).setUp()
+    self.saved_stdout, sys.stdout = sys.stdout, StringIO.StringIO()
+    self.option_mock = self.mox.CreateMock(optparse.Values)
+    self.option_mock.use_absolute_failcnt = False
+    self.option_mock.storepath = 'testdata'
+
+  def tearDown(self):
+    sys.stdout = self.saved_stdout
+
+  def test___main___wrongargs(self):
+    """Test the __main__ function -- wrong args."""
+    optionsparser_mock = self.mox.CreateMock(optparse.OptionParser)
+    self.mox.StubOutWithMock(check_openvz_beans, 'create_optionsparser')
+    check_openvz_beans.create_optionsparser().AndReturn(optionsparser_mock)
+    optionsparser_mock.parse_args().AndReturn((self.option_mock, ['arg',]))
+    optionsparser_mock.error('incorrect number of arguments').AndRaise(SystemExit)
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+
+  def test___main___nodata(self):
+    """Test the __main__ function -- no data."""
+    optionsparser_mock = self.mox.CreateMock(optparse.OptionParser)
+    self.mox.StubOutWithMock(check_openvz_beans, 'create_optionsparser')
+    check_openvz_beans.create_optionsparser().AndReturn(optionsparser_mock)
+    optionsparser_mock.parse_args().AndReturn((self.option_mock, []))
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'read_data')
+    check_openvz_beans.read_data(mox.IsA(optparse.Values)).AndReturn('')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: No data given while reading beancounters.\n')
+
+  def test___main___nobeancounters(self):
+    """Test the __main__ function -- no beancounters"""
+    optionsparser_mock = self.mox.CreateMock(optparse.OptionParser)
+    self.mox.StubOutWithMock(check_openvz_beans, 'create_optionsparser')
+    check_openvz_beans.create_optionsparser().AndReturn(optionsparser_mock)
+    optionsparser_mock.parse_args().AndReturn((self.option_mock, []))
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'read_data')
+    check_openvz_beans.read_data(mox.IsA(optparse.Values)).AndReturn('somedata')
+
+    beancounterparser_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterParser)
+    self.mox.StubOutWithMock(check_openvz_beans, 'BeanCounterParser')
+    check_openvz_beans.BeanCounterParser('somedata', False, 'testdata').AndRaise(check_openvz_beans.NoBeanCountersFoundError)
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: No beancounters found in data read.\n')
+
+
+class MainRegularTest(mox.MoxTestBase):
+  def setUp(self):
+    super(MainRegularTest, self).setUp()
+    self.saved_stdout, sys.stdout = sys.stdout, StringIO.StringIO()
+
+    self.optionsparser_mock = self.mox.CreateMock(optparse.OptionParser)
+    self.option_mock = self.mox.CreateMock(optparse.Values)
+    self.option_mock.confpath = 'irrelevant'
+    self.option_mock.domainparts = 21
+    self.option_mock.use_absolute_failcnt = False
+    self.option_mock.storepath = 'testdata'
+    self.option_mock.use_maxheld = False
+    self.mox.StubOutWithMock(check_openvz_beans, 'create_optionsparser')
+    check_openvz_beans.create_optionsparser().AndReturn(self.optionsparser_mock)
+    self.optionsparser_mock.parse_args().AndReturn((self.option_mock, []))
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'read_data')
+    check_openvz_beans.read_data(mox.IsA(optparse.Values)).AndReturn('somedata')
+
+    self.beancounterparser_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterParser)
+    self.mox.StubOutWithMock(check_openvz_beans, 'BeanCounterParser')
+    check_openvz_beans.BeanCounterParser('somedata', False, 'testdata').AndReturn(self.beancounterparser_mock)
+    self.beancounterparser_mock.get_beancounters().AndReturn(None)
+
+    self.beancounterverifier_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterVerifier)
+    self.mox.StubOutWithMock(check_openvz_beans, 'BeanCounterVerifier')
+    check_openvz_beans.BeanCounterVerifier(mox.IsA(optparse.Values)).AndReturn(self.beancounterverifier_mock)
+
+  def tearDown(self):
+    sys.stdout = self.saved_stdout
+
+  def test___main___noalerts(self):
+    """Test the __main__ function -- no alerts."""
+    self.beancounterverifier_mock.verify(None, False).AndReturn([])
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'OK: all beancounters below configured thresholds\n')
+
+  def test___main___nospecificalert(self):
+    """Test the __main__ function -- no alerts."""
+    beancounteralert_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterViolation)
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_mock,])
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'OK: all beancounters below configured thresholds\n')
+
+  def test___main___failalert(self):
+    """Test the __main__ function -- fail alert."""
+    beancounteralert_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterFailure)
+    beancounteralert_mock.uid = 123
+    beancounteralert_mock.excess = 987
+    beancounteralert_mock.resource = 'resource'
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_mock,])
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_hostname')
+    check_openvz_beans.get_hostname(123, 'irrelevant', 21).AndReturn('dummy')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: FAIL: dummy(123)| FAIL: HOST dummy(123): resource(+987)\n')
+
+  def test___main___overalert(self):
+    """Test the __main__ function -- over alert."""
+    beancounteralert_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterOvershoot)
+    beancounteralert_mock.uid = 123
+    beancounteralert_mock.excess = 987
+    beancounteralert_mock.resource = 'resource'
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_mock,])
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_hostname')
+    check_openvz_beans.get_hostname(123, 'irrelevant', 21).AndReturn('dummy')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: OVER: dummy(123)| OVER: HOST dummy(123): resource(+987)\n')
+
+  def test___main___critalert(self):
+    """Test the __main__ function -- crit alert."""
+    beancounteralert_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterCritical)
+    beancounteralert_mock.uid = 123
+    beancounteralert_mock.excess = 987
+    beancounteralert_mock.resource = 'resource'
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_mock,])
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_hostname')
+    check_openvz_beans.get_hostname(123, 'irrelevant', 21).AndReturn('dummy')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: CRIT: dummy(123)| CRIT: HOST dummy(123): resource(+987)\n')
+
+  def test___main___warnalert(self):
+    """Test the __main__ function -- warn alert."""
+    beancounteralert_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterWarning)
+    beancounteralert_mock.uid = 123
+    beancounteralert_mock.excess = 987
+    beancounteralert_mock.resource = 'resource'
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_mock,])
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_hostname')
+    check_openvz_beans.get_hostname(123, 'irrelevant', 21).AndReturn('dummy')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'WARNING: WARN: dummy(123)| WARN: HOST dummy(123): resource(+987)\n')
+
+  def test___main___failwarnalert(self):
+    """Test the __main__ function -- failwarn alert."""
+    beancounteralert_fail_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterFailure)
+    beancounteralert_fail_mock.uid = 123
+    beancounteralert_fail_mock.excess = 987
+    beancounteralert_fail_mock.resource = 'resource'
+    beancounteralert_warn_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterWarning)
+    beancounteralert_warn_mock.uid = 234
+    beancounteralert_warn_mock.excess = 896
+    beancounteralert_warn_mock.resource = 'resource'
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_fail_mock, beancounteralert_warn_mock])
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_hostname')
+    check_openvz_beans.get_hostname(123, 'irrelevant', 21).AndReturn('foo')
+    check_openvz_beans.get_hostname(234, 'irrelevant', 21).AndReturn('bar')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: FAIL: foo(123) WARN: bar(234)| FAIL: HOST foo(123): resource(+987) WARN: HOST bar(234): resource(+896)\n')
+
+  def test___main___multifailwarnalert(self):
+    """Test the __main__ function -- failwarn alert."""
+    beancounteralert_fail1_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterFailure)
+    beancounteralert_fail1_mock.uid = 123
+    beancounteralert_fail1_mock.excess = 987
+    beancounteralert_fail1_mock.resource = 'resource1'
+    beancounteralert_fail2_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterFailure)
+    beancounteralert_fail2_mock.uid = 123
+    beancounteralert_fail2_mock.excess = 987
+    beancounteralert_fail2_mock.resource = 'resource2'
+    beancounteralert_warn1_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterWarning)
+    beancounteralert_warn1_mock.uid = 234
+    beancounteralert_warn1_mock.excess = 896
+    beancounteralert_warn1_mock.resource = 'resource'
+    beancounteralert_warn2_mock = self.mox.CreateMock(check_openvz_beans.BeanCounterWarning)
+    beancounteralert_warn2_mock.uid = 345
+    beancounteralert_warn2_mock.excess = 896
+    beancounteralert_warn2_mock.resource = 'resource'
+    self.beancounterverifier_mock.verify(None, False).AndReturn([beancounteralert_fail1_mock, beancounteralert_fail2_mock, beancounteralert_warn1_mock, beancounteralert_warn2_mock])
+
+    self.mox.StubOutWithMock(check_openvz_beans, 'get_hostname')
+    check_openvz_beans.get_hostname(123, 'irrelevant', 21).AndReturn('foo')
+    check_openvz_beans.get_hostname(345, 'irrelevant', 21).AndReturn('baz')
+    check_openvz_beans.get_hostname(234, 'irrelevant', 21).AndReturn('bar')
+
+    self.mox.ReplayAll()
+
+    # FIXME: check the Exception value. assertRaisesRegexp() is only available in Python unittest 2.7+
+    self.assertRaises(SystemExit, check_openvz_beans.__main__)
+    self.assertEqual(sys.stdout.getvalue(), 'CRITICAL: FAIL: foo(123) WARN: baz(345) bar(234)| FAIL: HOST foo(123): resource1(+987) resource2(+987) WARN: HOST baz(345): resource(+896) HOST bar(234): resource(+896)\n')
+
+if __name__ == '__main__':
+  unittest.main()
diff -urN nothing/debian/changelog nagios-plugins-openvz-beans/debian/changelog
--- nothing/debian/changelog	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/debian/changelog	2011-11-01 18:30:10.247574999 +0100
@@ -0,0 +1,5 @@
+nagios-plugins-openvz-beans (0.5-0) stable; urgency=low
+
+  * initial release packaging
+
+ -- Andreas Kotes <andreas.kotes@nokia.com>  Thu, 01 Nov 2011 18:30:23 +0100
diff -urN nothing/debian/compat nagios-plugins-openvz-beans/debian/compat
--- nothing/debian/compat	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/debian/compat	2011-10-17 14:23:00.655695188 +0200
@@ -0,0 +1 @@
+5
diff -urN nothing/debian/control nagios-plugins-openvz-beans/debian/control
--- nothing/debian/control	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/debian/control	2011-11-01 16:54:48.381618568 +0100
@@ -0,0 +1,9 @@
+Source: nagios-plugins-openvz-beans
+Section: net
+Priority: extra
+Maintainer: Andreas Kotes <andreas.kotes@nokia.com>
+
+Package: nagios-plugins-openvz-beans
+Architecture: all
+Depends: python-minimal
+Description: Nagios Plugin to check OpenVZ Bean Counters
diff -urN nothing/debian/.gitignore nagios-plugins-openvz-beans/debian/.gitignore
--- nothing/debian/.gitignore	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/debian/.gitignore	2011-10-17 14:23:00.655695188 +0200
@@ -0,0 +1,4 @@
+files
+nagios-plugins-openvz-beans.debhelper.log
+nagios-plugins-openvz-beans.substvars
+nagios-plugins-openvz-beans/
diff -urN nothing/debian/rules nagios-plugins-openvz-beans/debian/rules
--- nothing/debian/rules	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/debian/rules	2011-11-01 18:28:09.727238903 +0100
@@ -0,0 +1,33 @@
+#!/usr/bin/make -f
+DEBIANDIR=$(CURDIR)/debian
+
+%:
+	dh $@
+
+install: build
+	dh_testdir
+	dh_testroot
+	dh_clean -k
+	dh_installdirs
+
+	install -d -m0755 $(DEBIANDIR)/nagios-plugins-openvz-beans/usr/lib/nagios/plugins
+	install -m0755 check_openvz_beans.py $(DEBIANDIR)/nagios-plugins-openvz-beans/usr/lib/nagios/plugins
+	install -d -m0755 $(DEBIANDIR)/nagios-plugins-openvz-beans/usr/share/doc/nagios-plugins-openvz-beans
+	install -m0644 README $(DEBIANDIR)/nagios-plugins-openvz-beans/usr/share/doc/nagios-plugins-openvz-beans
+
+# Build architecture-independent files here.
+binary-indep: build install
+	dh_testdir -i
+	dh_testroot -i
+	dh_installchangelogs -i
+	dh_installdocs -i
+	dh_link -i
+	dh_compress -i
+	dh_fixperms -i
+	dh_installdeb -i
+	dh_gencontrol -i
+	dh_md5sums -i
+	dh_builddeb -i
+
+binary: binary-indep
+.PHONY: build clean binary-indep binary install
diff -urN nothing/.gitignore nagios-plugins-openvz-beans/.gitignore
--- nothing/.gitignore	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/.gitignore	2011-10-17 14:23:00.695695306 +0200
@@ -0,0 +1 @@
+*.pyc
diff -urN nothing/nagios-plugins-openvz-beans.spec nagios-plugins-openvz-beans/nagios-plugins-openvz-beans.spec
--- nothing/nagios-plugins-openvz-beans.spec	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/nagios-plugins-openvz-beans.spec	2011-11-01 16:54:35.621582988 +0100
@@ -0,0 +1,34 @@
+Name:          nagios-plugins-openvz-beans
+Version:       0.5
+Release:       0
+Summary:       Nagios Plugin to check OpenVZ Bean Counters
+License:       GPL
+Group:         Applications/System
+Source:        check_openvz_beans.py
+
+Autoreq:       0
+BuildRoot:     %{_tmppath}/%{name}-%{version}-%{release}-root
+BuildArch:     noarch
+Requires:      python
+
+%description
+Nagios Plugin to check OpenVZ Bean Counters
+
+%prep
+rm -rf %{buildroot}
+
+%install
+install -d -m0755 %{buildroot}%{_libdir}/nagios/plugins/contrib/
+install -m0755 %{_sourcefile} %{buildroot}%{_libdir}/nagios/plugins/contrib/
+
+
+%clean
+rm -rf %{buildroot}
+
+%files
+%dir %{_libdir}/nagios/plugins/contrib
+%attr(755, root, root) %{_libdir}/nagios/plugins/contrib/check_openvz_beans.py
+
+%changelog
+* Thu Oct 13 2011 Andreas Kotes <andreas.kotes@nokia.com> 0.5
+  - initial release packaging
diff -urN nothing/README nagios-plugins-openvz-beans/README
--- nothing/README	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/README	2011-11-01 18:04:40.633309403 +0100
@@ -0,0 +1,75 @@
+Nagios check for OpenVZ bean counters
+=====================================
+
+General
+-------
+
+This Nagios plugin allows to define and check for thresholds against bean
+counters defined in /proc/user_beancounters, which are available in both OpenVZ
+hypervisors and VMs.
+
+It's possible to set global thresholds via '-c' (for critical) and '-w' (for
+warning) which apply to all resources checked.
+
+Tresholds for individual resources can be set by using '-C'/'-W' and giving
+comma-separated key:value pairs.
+
+The script was written to be compatible with Python 2.4 and later.
+
+Reading /proc/user_beancounters
+-------------------------------
+
+Since /proc/user_beancounters is only readable by the root user - and running
+Nagios plugins as the root user is highly discouraged - you can either copy
+this file and check against the copy using the -f option. Or - if you have set
+up your /etc/sudoers file to allow it - you can use the -s option, which lets
+check_openvz_beans use 'sudo cat /proc/user_beancounters' to read the file.
+
+Hostname handling
+-----------------
+The OpenVZ context IDs are resolved by grepping for the HOSTNAME variable in
+the uid's config file found in the path given by '-p'.
+
+Hostnames (e.g. this.is.a.very.long.hostname) are shortened to their first two
+components per default (e.g. this.is for the former one), but this length can
+be configured using -d (e.g. '-d 1' for 'this', '-d 3' for 'this.is.a'),
+allowing you to identify the affected VM(s) in the Nagios alert message
+already.
+
+Output
+------
+
+The output follows the Nagios Plugin API 3.0, i.e. the script gives the proper
+exit codes (0 = OK, 1 = Warning, 2 = Critical, 3 = Unknown), a simple single
+line of text, and additional perf-data.
+
+The single line of text informs about WARNING or CRITICAL state, and what's a
+problem:
+
+FAIL - a resource against its limit and the was blocked by kernel
+OVER - a resource was over its relevant threshold and will cause problems
+CRIT - a resource ran close to its threshold by given (critical) percentage
+WARN - a resource ran close to its threshold by given (warning) percentage
+
+Data used
+---------
+
+The value checked for each resource is the 'held' value, but you could use '-m'
+to use the 'maxheld' value instead. You'll not be able to reset that value,
+though, so any Nagios warning/failure status will stay until the next
+hypervisor reboot.
+
+Failure counters can't be reset either, but the plugin reads the data stored by
+vzubc's relative mode, i.e. if you inspect the problem with 'vzubc -r', the
+critical status will be resolved. This behaviour can be turned off with '-a'.
+
+If you are storing data somewhere else than the default '/tmp/vzubc.store', you
+can change that via '-u'.
+
+vzubc is provided by the vzctl package which isn't used by this code directly,
+nor is there a dependency - you'll have to install it yourself if you want to
+use this functionality.
+
+Each group lists the hostname & uid of the VM in the regular message,
+additionally the resource name and how much its threshold was exeeded are given
+in the perfdata line.
diff -urN nothing/README.source nagios-plugins-openvz-beans/README.source
--- nothing/README.source	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/README.source	2011-11-01 16:29:20.667358270 +0100
@@ -0,0 +1,16 @@
+The implementation can be tested by calling its test scripts
+./check_openvz_beans-test.py, which runs unit tests for almost all of its code,
+and requires Python unittest >= 2.4 as well as pymox.
+
+Current code coverage of the unit tests is >95%.
+
+RPMs can be build using the following commandline:
+
+rpmbuild -bb -D "_sourcefile `pwd`/check_openvz_beans.py" nagios-plugins-openvz-beans.spec
+
+Debian / Ubuntu packages can be build by simply calling:
+
+dpkg-buildpackage
+
+... in both cases the proper developer tools for the corresponding distribution
+have to be installed.
diff -urN nothing/testdata/10.conf nagios-plugins-openvz-beans/testdata/10.conf
--- nothing/testdata/10.conf	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/testdata/10.conf	2011-11-01 19:09:28.124150356 +0100
@@ -0,0 +1 @@
+HOSTNAME="vmokay.pr.foo.test.your.do.main"
diff -urN nothing/testdata/60.conf nagios-plugins-openvz-beans/testdata/60.conf
--- nothing/testdata/60.conf	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/testdata/60.conf	2011-11-01 19:09:32.834163485 +0100
@@ -0,0 +1 @@
+HOSTNAME="vmover1.st.foo.test.your.do.main"
diff -urN nothing/testdata/hostcrit.bcs nagios-plugins-openvz-beans/testdata/hostcrit.bcs
--- nothing/testdata/hostcrit.bcs	1970-01-01 01:00:00.000000000 +0100
+++ nagios-plugins-openvz-beans/testdata/hostcrit.bcs	2011-11-01 19:07:53.643886880 +0100
@@ -0,0 +1,170 @@
+Version: 2.5
+       uid  resource                     held              maxheld              barrier                limit              failcnt
+       40:  kmemsize                 12345678             33456789             34567890             40000000                    0
+            lockedpages                     0                 5859                32768                65536                    0
+            privvmpages               1234567              2345678              3276800  9223372036854775807                    0
+            shmpages                      688                 1344  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0                    0                    0                    0
+            numproc                        75                   88                 8000                 8000                    0
+            physpages                  199301               239798                    0  9223372036854775807                    0
+            vmguarpages                     0                    0              4194304  9223372036854775807                    0
+            oomguarpages               199301               239798              4186112  9223372036854775807                    0
+            numtcpsock                     16                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        4                   14                  200                  220                    0
+            numpty                          1                    2                   64                   64                    0
+            numsiginfo                      0                    9                  512                  512                    0
+            tcpsndbuf                  278016               547280              2703360  9223372036854775807                    0
+            tcprcvbuf                  262144               769968              2703360  9223372036854775807                    0
+            othersockbuf               158304               197424  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                58680               262144               262144                    0
+            numothersock                  115                  125                  360                  360                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      1655                 1958                 9312                 9312                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            numiptent                      20                   20                  128                  128                    0
+       20:  kmemsize                 12345678             23456789             34567890             40000000                    0
+            lockedpages                     0                 5859                32768                65536                    0
+            privvmpages               1234567              2345678              3276800  9223372036854775807                    3
+            shmpages                      688                 1344  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0                    0                    0                    0
+            numproc                        75                   88                 8000                 8000                    0
+            physpages                  199301               239798                    0  9223372036854775807                    0
+            vmguarpages                     0                    0              4194304  9223372036854775807                    0
+            oomguarpages               199301               239798              4186112  9223372036854775807                    0
+            numtcpsock                     16                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        4                   14                  200                  220                    0
+            numpty                          1                    2                   64                   64                    0
+            numsiginfo                      0                    9                  512                  512                    0
+            tcpsndbuf                  278016               547280              2703360  9223372036854775807                    0
+            tcprcvbuf                  262144               769968              2703360  9223372036854775807                    0
+            othersockbuf               158304               197424  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                58680               262144               262144                    0
+            numothersock                  115                  125                  360                  360                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      1655                 1958                 9312                 9312                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            numiptent                      20                   20                  128                  128                    0
+       30:  kmemsize                 12345678             23456789             34567890             40000000                    0
+            lockedpages                     0                 5859                32768                65536                    0
+            privvmpages               1234567              2345678              3276800  9223372036854775807                    3
+            shmpages                      688                 1344  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0                    0                    0                    0
+            numproc                        75                   88                  100                  120                    2
+            physpages                  199301               239798                    0  9223372036854775807                    0
+            vmguarpages                     0                    0              4194304  9223372036854775807                    0
+            oomguarpages               199301               239798              4186112  9223372036854775807                    0
+            numtcpsock                     16                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        4                   14                  200                  220                    0
+            numpty                          1                    2                   64                   64                    0
+            numsiginfo                      0                    9                  512                  512                    0
+            tcpsndbuf                  278016               547280              2703360  9223372036854775807                    0
+            tcprcvbuf                  262144               769968              2703360  9223372036854775807                    0
+            othersockbuf               158304               197424  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                58680               262144               262144                    0
+            numothersock                  115                  125                  360                  360                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      1655                 1958                 9312                 9312                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            numiptent                      20                   20                  128                  128                    0
+       10:  kmemsize                 12345678             30456789             34567890             40000000                    0
+            lockedpages                     0                 5859                32768                65536                    0
+            privvmpages               1234567              2345678              3276800  9223372036854775807                    0
+            shmpages                      688                 1344  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0                    0                    0                    0
+            numproc                        75                   88                 8000                 8000                    0
+            physpages                  199301               239798                    0  9223372036854775807                    0
+            vmguarpages                     0                    0              4194304  9223372036854775807                    0
+            oomguarpages               199301               239798              4186112  9223372036854775807                    0
+            numtcpsock                     16                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        4                   14                  200                  220                    0
+            numpty                          1                    2                   64                   64                    0
+            numsiginfo                      0                    9                  512                  512                    0
+            tcpsndbuf                  278016               547280              2703360  9223372036854775807                    0
+            tcprcvbuf                  262144               769968              2703360  9223372036854775807                    0
+            othersockbuf               158304               197424  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                58680               262144               262144                    0
+            numothersock                  115                  125                  360                  360                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      1655                 1958                 9312                 9312                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            numiptent                      20                   20                  128                  128                    0
+       50:  kmemsize                 12345678             31456789             34567890             40000000                    0
+            lockedpages                     0                 5859                32768                65536                    0
+            privvmpages               1234567              2345678              3276800  9223372036854775807                    0
+            shmpages                      688                 1344  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0                    0                    0                    0
+            numproc                        75                   88                 8000                 8000                    0
+            physpages                  199301               239798                    0  9223372036854775807                    0
+            vmguarpages                     0                    0              4194304  9223372036854775807                    0
+            oomguarpages               199301               239798              4186112  9223372036854775807                    0
+            numtcpsock                     16                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        4                   14                  200                  220                    0
+            numpty                          1                    2                   64                   64                    0
+            numsiginfo                      0                    9                  512                  512                    0
+            tcpsndbuf                  278016               547280              2703360  9223372036854775807                    0
+            tcprcvbuf                  262144               769968              2703360  9223372036854775807                    0
+            othersockbuf               158304               197424  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                58680               262144               262144                    0
+            numothersock                  115                  125                  360                  360                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      1655                 1958                 9312                 9312                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            numiptent                      20                   20                  128                  128                    0
+       60:  kmemsize                 12345678             40000001             34567890             40000000                    0
+            lockedpages                     0                 5859                32768                65536                    0
+            privvmpages               1234567              2345678              3276800  9223372036854775807                    0
+            shmpages                      688                 1344  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0                    0                    0                    0
+            numproc                        75                   88                 8000                 8000                    0
+            physpages                  199301               239798                    0  9223372036854775807                    0
+            vmguarpages                     0                    0              4194304  9223372036854775807                    0
+            oomguarpages               199301               239798              4186112  9223372036854775807                    0
+            numtcpsock                     16                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        4                   14                  200                  220                    0
+            numpty                          1                    2                   64                   64                    0
+            numsiginfo                      0                    9                  512                  512                    0
+            tcpsndbuf                  278016               547280              2703360  9223372036854775807                    0
+            tcprcvbuf                  262144               769968              2703360  9223372036854775807                    0
+            othersockbuf               158304               197424  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                58680               262144               262144                    0
+            numothersock                  115                  125                  360                  360                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      1655                 1958                 9312                 9312                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            dummy                           0                    0                    0                    0                    0
+            numiptent                      20                   20                  128                  128                    0
+        0:  kmemsize                 15408521             33415719  9223372036854775807  9223372036854775807                    0
+            lockedpages                  8789                12353  9223372036854775807  9223372036854775807                    0
+            privvmpages                 41072               104930  9223372036854775807  9223372036854775807                    0
+            shmpages                      730                 1706  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0  9223372036854775807  9223372036854775807                    0
+            numproc                       291                  472  9223372036854775807  9223372036854775807                    0
+            physpages                    9432                54303  9223372036854775807  9223372036854775807                    0
+            vmguarpages                     0                    0  9223372036854775807  9223372036854775807                    0
+            oomguarpages                 9432                54303  9223372036854775807  9223372036854775807                    0
+            numtcpsock                     13                   21  9223372036854775807  9223372036854775807                    0
+            numflock                        5                   22  9223372036854775807  9223372036854775807                    0
+            numpty                          1                    1  9223372036854775807  9223372036854775807                    0
+            numsiginfo                      0                    6  9223372036854775807  9223372036854775807                    0
+            tcpsndbuf                  773048               924800  9223372036854775807  9223372036854775807                    0
+            tcprcvbuf                  212992               854408  9223372036854775807  9223372036854775807                    0
+            othersockbuf               204864               283784  9223372036854775807  9223372036854775807                    0
+            dgramrcvbuf                     0                16944  9223372036854775807  9223372036854775807                    0
+            numothersock                  178                  225  9223372036854775807  9223372036854775807                    0
+            dcachesize                      0                    0  9223372036854775807  9223372036854775807                    0
+            numfile                      5141                 6779  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0  9223372036854775807  9223372036854775807                    0
+            dummy                           0                    0  9223372036854775807  9223372036854775807                    0
+            numiptent                      51                   51  9223372036854775807  9223372036854775807                    0