File: sanoid

package info (click to toggle)
sanoid 2.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 592 kB
  • sloc: perl: 2,550; sh: 367; makefile: 25
file content (1678 lines) | stat: -rwxr-xr-x 55,897 bytes parent folder | download
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
#!/usr/bin/perl

# this software is licensed for use under the Free Software Foundation's GPL v3.0 license, as retrieved
# from http://www.gnu.org/licenses/gpl-3.0.html on 2014-11-17.  A copy should also be available in this
# project's Git repository at https://github.com/jimsalterjrs/sanoid/blob/master/LICENSE.

$::VERSION = '2.0.3';
my $MINIMUM_DEFAULTS_VERSION = 2;

use strict;
use warnings;
use Config::IniFiles; # read samba-style conf file
use Data::Dumper;     # debugging - print contents of hash
use File::Path;       # for rmtree command in use_prune
use Getopt::Long qw(:config auto_version auto_help);
use Pod::Usage;       # pod2usage
use Time::Local;      # to parse dates in reverse
use Capture::Tiny ':all';

my %args = ("configdir" => "/etc/sanoid");
GetOptions(\%args, "verbose", "debug", "cron", "readonly", "quiet",
                   "monitor-health", "force-update", "configdir=s",
                   "monitor-snapshots", "take-snapshots", "prune-snapshots", "force-prune",
                   "monitor-capacity"
          ) or pod2usage(2);

# If only config directory (or nothing) has been specified, default to --cron --verbose
if (keys %args < 2) {
	$args{'cron'} = 1;
	$args{'verbose'} = 1;
}

my $pscmd = '/bin/ps';

my $zfs = '/sbin/zfs';
my $zpool = '/sbin/zpool';

my $conf_file = "$args{'configdir'}/sanoid.conf";
my $default_conf_file = "/usr/share/sanoid/sanoid.defaults.conf";

# parse config file
my %config = init($conf_file,$default_conf_file);

# if we call getsnaps(%config,1) it will forcibly update the cache, TTL or no TTL
my $forcecacheupdate = 0;
my $cache = '/var/cache/sanoidsnapshots.txt';
my $cacheTTL = 900; # 15 minutes
my %snaps = getsnaps( \%config, $cacheTTL, $forcecacheupdate );
my %pruned;
my %capacitycache;

my %snapsbytype = getsnapsbytype( \%config, \%snaps );

my %snapsbypath = getsnapsbypath( \%config, \%snaps );

# let's make it a little easier to be consistent passing these hashes in the same order to each sub
my @params = ( \%config, \%snaps, \%snapsbytype, \%snapsbypath );

if ($args{'debug'})		{ $args{'verbose'}=1; blabber (@params); }
if ($args{'monitor-snapshots'}) { monitor_snapshots(@params); }
if ($args{'monitor-health'}) 	{ monitor_health(@params); }
if ($args{'monitor-capacity'})  { monitor_capacity(@params); }
if ($args{'force-update'}) 	{ my $snaps = getsnaps( \%config, $cacheTTL, 1 ); }

if ($args{'cron'}) {
	if ($args{'quiet'}) { $args{'verbose'} = 0; }
	take_snapshots (@params);
	prune_snapshots (@params);
} else {
	if ($args{'take-snapshots'}) { take_snapshots (@params); }
	if ($args{'prune-snapshots'}) { prune_snapshots (@params); }
}

exit 0;


####################################################################################
####################################################################################
####################################################################################

sub monitor_health {
	my ($config, $snaps, $snapsbytype, $snapsbypath) = @_;
	my %pools;
	my @messages;
	my $errlevel=0;

	foreach my $path (keys %{ $snapsbypath}) {
		my @pool = split ('/',$path);
		$pools{$pool[0]}=1;
	}

	foreach my $pool (keys %pools) {
		my ($exitcode, $msg) = check_zpool($pool,2);
		if ($exitcode > $errlevel) { $errlevel = $exitcode; }
		chomp $msg;
		push (@messages, $msg);
	}

	my @warninglevels = ('','*** WARNING *** ','*** CRITICAL *** ');
	my $message = $warninglevels[$errlevel] . join (', ',@messages);
	print "$message\n";
	exit $errlevel;

}

####################################################################################
####################################################################################
####################################################################################

sub monitor_snapshots {

	# nagios plugin format: exit 0,1,2,3 for OK, WARN, CRITICAL, or ERROR.

	# check_snapshot_date - test ZFS fs creation timestamp for recentness
	# accepts arguments: $filesystem, $warn (in seconds elapsed), $crit (in seconds elapsed)

	my ($config, $snaps, $snapsbytype, $snapsbypath) = @_;
	my %datestamp = get_date();
	my $errorlevel = 0;
	my $msg;
	my @msgs;
	my @paths;

	foreach my $section (keys %config) {
		if ($section =~ /^template/) { next; }
		if (! $config{$section}{'monitor'}) { next; }
		if ($config{$section}{'process_children_only'}) { next; }

		my $path = $config{$section}{'path'};
		push @paths, $path;

		my @types = ('yearly','monthly','weekly','daily','hourly','frequently');
		foreach my $type (@types) {
			if ($config{$section}{$type} == 0) { next; }

			my $smallerperiod = 0;
			# we need to set the period length in seconds first
			if ($type eq 'frequently') { $smallerperiod = 1; }
			elsif ($type eq 'hourly') { $smallerperiod = 60; }
			elsif ($type eq 'daily') { $smallerperiod = 60*60; }
			elsif ($type eq 'weekly') { $smallerperiod = 60*60*24; }
			elsif ($type eq 'monthly') { $smallerperiod = 60*60*24*7; }
			elsif ($type eq 'yearly') { $smallerperiod = 60*60*24*31; }

			my $typewarn = $type . '_warn';
			my $typecrit = $type . '_crit';
			my $warn = convertTimePeriod($config{$section}{$typewarn}, $smallerperiod);
			my $crit = convertTimePeriod($config{$section}{$typecrit}, $smallerperiod);
			my $elapsed = -1;
			if (defined $snapsbytype{$path}{$type}{'newest'}) {
				$elapsed = $snapsbytype{$path}{$type}{'newest'};
			}
			my $dispelapsed = displaytime($elapsed);
			my $dispwarn = displaytime($warn);
			my $dispcrit = displaytime($crit);
			if ( $elapsed > $crit || $elapsed == -1) {
				if ($crit > 0) {
					if (! $config{$section}{'monitor_dont_crit'}) { $errorlevel = 2; }
					if ($elapsed == -1) {
						push @msgs, "CRIT: $path has no $type snapshots at all!";
					} else {
						push @msgs, "CRIT: $path\'s newest $type snapshot is $dispelapsed old (should be < $dispcrit)";
					}
				}
			 } elsif ($elapsed > $warn) {
				if ($warn > 0) {
					if (! $config{$section}{'monitor_dont_warn'} && ($errorlevel < 2) ) { $errorlevel = 1; }
					push @msgs, "WARN: $path\'s newest $type snapshot is $dispelapsed old (should be < $dispwarn)";
				}
			} else {
				# push @msgs .= "OK: $path\'s newest $type snapshot is $dispelapsed old \n";
			}

		}
	}

	my @sorted_msgs = sort { lc($a) cmp lc($b) } @msgs;
	my @sorted_paths = sort { lc($a) cmp lc($b) } @paths;
	$msg = join (", ", @sorted_msgs);
	my $paths = join (", ", @sorted_paths);

	if ($msg eq '') { $msg = "OK: all monitored datasets \($paths\) have fresh snapshots"; }

	print "$msg\n";
	exit $errorlevel;
}


####################################################################################
####################################################################################
####################################################################################

sub monitor_capacity {
	my ($config, $snaps, $snapsbytype, $snapsbypath) = @_;
	my %pools;
	my @messages;
	my $errlevel=0;

	# build pool list with corresponding capacity limits
	foreach my $section (keys %config) {
		my @pool = split ('/',$section);

		if (scalar @pool == 1 || !defined($pools{$pool[0]}) ) {
			my %capacitylimits;

			if (!check_capacity_limit($config{$section}{'capacity_warn'})) {
				die "ERROR: invalid zpool capacity warning limit!\n";
			}

			if ($config{$section}{'capacity_warn'} != 0) {
				$capacitylimits{'warn'} = $config{$section}{'capacity_warn'};
			}

			if (!check_capacity_limit($config{$section}{'capacity_crit'})) {
				die "ERROR: invalid zpool capacity critical limit!\n";
			}

			if ($config{$section}{'capacity_crit'} != 0) {
				$capacitylimits{'crit'} = $config{$section}{'capacity_crit'};
			}

			if (%capacitylimits) {
				$pools{$pool[0]} = \%capacitylimits;
			}
		}
	}

	foreach my $pool (keys %pools) {
		my $capacitylimitsref = $pools{$pool};

		my ($exitcode, $msg) = check_zpool_capacity($pool,\%$capacitylimitsref);
		if ($exitcode > $errlevel) { $errlevel = $exitcode; }
		chomp $msg;
		push (@messages, $msg);
	}

	my @warninglevels = ('','*** WARNING *** ','*** CRITICAL *** ');
	my $message = $warninglevels[$errlevel] . join (', ',@messages);
	print "$message\n";
	exit $errlevel;
}

####################################################################################
####################################################################################
####################################################################################


sub prune_snapshots {

	if ($args{'verbose'}) { print "INFO: pruning snapshots...\n"; }
	my ($config, $snaps, $snapsbytype, $snapsbypath) = @_;

	my %datestamp = get_date();
	my $forcecacheupdate = 0;

	foreach my $section (keys %config) {
		if ($section =~ /^template/) { next; }
		if (! $config{$section}{'autoprune'}) { next; }
		if ($config{$section}{'process_children_only'}) { next; }

		my $path = $config{$section}{'path'};

		my $period = 0;
		if (check_prune_defer($config, $section)) {
			if ($args{'verbose'}) { print "INFO: deferring snapshot pruning ($section)...\n"; }
			next;
		}

		foreach my $type (keys %{ $config{$section} }){
			unless ($type =~ /ly$/) { next; }

			# we need to set the period length in seconds first
			if ($type eq 'frequently') { $period = 60 * $config{$section}{'frequent_period'}; }
			elsif ($type eq 'hourly') { $period = 60*60; }
			elsif ($type eq 'daily') { $period = 60*60*24; }
			elsif ($type eq 'weekly') { $period = 60*60*24*7; }
			elsif ($type eq 'monthly') { $period = 60*60*24*31; }
			elsif ($type eq 'yearly') { $period = 60*60*24*365.25; }

			# avoid pissing off use warnings by not executing this block if no matching snaps exist
			if (defined $snapsbytype{$path}{$type}{'sorted'}) {
				my @sorted = split (/\|/,$snapsbytype{$path}{$type}{'sorted'});

				# if we say "daily=30" we really mean "don't keep any dailies more than 30 days old", etc
				my $maxage = ( time() - $config{$section}{$type} * $period );
				# but if we say "daily=30" we ALSO mean "don't get rid of ANY dailies unless we have more than 30".
				my $minsnapsthistype = $config{$section}{$type};

				# how many total snaps of this type do we currently have?
				my $numsnapsthistype = scalar (@sorted);

				my @prunesnaps;
				foreach my $snap( @sorted ){
					# print "snap $path\@$snap has age $snaps{$path}{$snap}{'ctime'}, maxage is $maxage.\n";
					if ( ($snaps{$path}{$snap}{'ctime'} < $maxage) && ($numsnapsthistype > $minsnapsthistype) ) {
						my $fullpath = $path . '@' . $snap;
						push(@prunesnaps,$fullpath);
						# we just got rid of a snap, so we now have one fewer, duh
						$numsnapsthistype--;
					}
				}

				if ((scalar @prunesnaps) > 0) {
					# print "found some snaps to prune!\n"
					if (checklock('sanoid_pruning')) {
						writelock('sanoid_pruning');
						foreach my $snap( @prunesnaps ){
							if ($args{'verbose'}) { print "INFO: pruning $snap ... \n"; }
							if (!$args{'force-prune'} && iszfsbusy($path)) {
								if ($args{'verbose'}) { print "INFO: deferring pruning of $snap - $path is currently in zfs send or receive.\n"; }
							} else {
								if (! $args{'readonly'}) {
									if (system($zfs, "destroy", $snap) == 0) {
										$pruned{$snap} = 1;
										my $dataset = (split '@', $snap)[0];
										my $snapname = (split '@', $snap)[1];
										if ($config{$dataset}{'pruning_script'}) {
											$ENV{'SANOID_TARGET'} = $dataset;
											$ENV{'SANOID_SNAPNAME'} = $snapname;
											if ($args{'verbose'}) { print "executing pruning_script '".$config{$dataset}{'pruning_script'}."' on dataset '$dataset'\n"; }
											my $ret = runscript('pruning_script',$dataset);

											delete $ENV{'SANOID_TARGET'};
											delete $ENV{'SANOID_SNAPNAME'};
										}
									} else {
										warn "could not remove $snap : $?";
									}
								}
							}
						}
						removelock('sanoid_pruning');
						removecachedsnapshots(0);
					} else {
						if ($args{'verbose'}) { print "INFO: deferring snapshot pruning - valid pruning lock held by other sanoid process.\n"; }
					}
				}
			}
		}
	}

	# if there were any deferred cache updates,
	# do them now and wait if necessary
	removecachedsnapshots(1);
} # end prune_snapshots


####################################################################################
####################################################################################
####################################################################################

sub take_snapshots {

	my ($config, $snaps, $snapsbytype, $snapsbypath) = @_;

	my %datestamp = get_date();
	my $forcecacheupdate = 0;

	my @newsnaps;

	# get utc timestamp of the current day for DST check
	my $daystartUtc = timelocal(0, 0, 0, $datestamp{'mday'}, ($datestamp{'mon'}-1), $datestamp{'year'});
	my ($isdst) = (localtime($daystartUtc))[8];
	my $dstOffset = 0;

	if ($isdst ne $datestamp{'isdst'}) {
		# current dst is different then at the beginning og the day
		if ($isdst) {
			# DST ended in the current day
			$dstOffset = 60*60;
		}
	}

	if ($args{'verbose'}) { print "INFO: taking snapshots...\n"; }
	foreach my $section (keys %config) {
		if ($section =~ /^template/) { next; }
		if (! $config{$section}{'autosnap'}) { next; }
		if ($config{$section}{'process_children_only'}) { next; }

		my $path = $config{$section}{'path'};
                my @types = ('yearly','monthly','weekly','daily','hourly','frequently');

                foreach my $type (@types) {
			if ($config{$section}{$type} > 0) {

				my $newestage; # in seconds
				if (defined $snapsbytype{$path}{$type}{'newest'}) {
					$newestage = $snapsbytype{$path}{$type}{'newest'};
				} else{
					$newestage = 9999999999999999;
				}

				# for use with localtime: @preferredtime will be most recent preferred snapshot time in ($sec,$min,$hour,$mon-1,$year) format
				my @preferredtime;
				my $lastpreferred;

				# to avoid duplicates with DST
				my $handleDst = 0;

				if ($type eq 'frequently')     {
					my $frequentslice = int($datestamp{'min'} / $config{$section}{'frequent_period'});

					push @preferredtime,0; # try to hit 0 seconds
					push @preferredtime,$frequentslice * $config{$section}{'frequent_period'};
					push @preferredtime,$datestamp{'hour'};
					push @preferredtime,$datestamp{'mday'};
					push @preferredtime,($datestamp{'mon'}-1); # january is month 0
					push @preferredtime,$datestamp{'year'};
					$lastpreferred = timelocal(@preferredtime);
					if ($lastpreferred > time()) { $lastpreferred -= 60 * $config{$section}{'frequent_period'}; } # preferred time is later this frequent period - so look at last frequent period
				} elsif ($type eq 'hourly')     {
					push @preferredtime,0; # try to hit 0 seconds
					push @preferredtime,$config{$section}{'hourly_min'};
					push @preferredtime,$datestamp{'hour'};
					push @preferredtime,$datestamp{'mday'};
					push @preferredtime,($datestamp{'mon'}-1); # january is month 0
					push @preferredtime,$datestamp{'year'};
					$lastpreferred = timelocal(@preferredtime);

					if ($dstOffset ne 0) {
						# timelocal doesn't take DST into account
						$lastpreferred += $dstOffset;
						# DST ended, avoid duplicates
						$handleDst = 1;
					}
					if ($lastpreferred > time()) { $lastpreferred -= 60*60; } # preferred time is later this hour - so look at last hour's
				} elsif ($type eq 'daily')   {
					push @preferredtime,0; # try to hit 0 seconds
					push @preferredtime,$config{$section}{'daily_min'};
					push @preferredtime,$config{$section}{'daily_hour'};
					push @preferredtime,$datestamp{'mday'};
					push @preferredtime,($datestamp{'mon'}-1); # january is month 0
					push @preferredtime,$datestamp{'year'};
					$lastpreferred = timelocal(@preferredtime);

					# timelocal doesn't take DST into account
					$lastpreferred += $dstOffset;

					# check if the planned time has different DST flag than the current
					my ($isdst) = (localtime($lastpreferred))[8];
					if ($isdst ne $datestamp{'isdst'}) {
						if (!$isdst) {
							# correct DST difference
							$lastpreferred -= 60*60;
						}
					}

					if ($lastpreferred > time()) {
						$lastpreferred -= 60*60*24;

						if ($dstOffset ne 0) {
							# because we are going back one day
							# the DST difference has to be accounted
							# for in reverse now
							$lastpreferred -= 2*$dstOffset;
						}
					} # preferred time is later today - so look at yesterday's
				} elsif ($type eq 'weekly')   {
					# calculate offset in seconds for the desired weekday
					my $offset = 0;
					if ($config{$section}{'weekly_wday'} < $datestamp{'wday'}) {
						$offset += 7;
					}
					$offset += $config{$section}{'weekly_wday'} - $datestamp{'wday'};
					$offset *= 60*60*24; # full day

					push @preferredtime,0; # try to hit 0 seconds
					push @preferredtime,$config{$section}{'weekly_min'};
					push @preferredtime,$config{$section}{'weekly_hour'};
					push @preferredtime,$datestamp{'mday'};
					push @preferredtime,($datestamp{'mon'}-1); # january is month 0
					push @preferredtime,$datestamp{'year'};
					$lastpreferred = timelocal(@preferredtime);
					$lastpreferred += $offset;
					if ($lastpreferred > time()) { $lastpreferred -= 60*60*24*7; } # preferred time is later this week - so look at last week's
				} elsif ($type eq 'monthly') {
					push @preferredtime,0; # try to hit 0 seconds
					push @preferredtime,$config{$section}{'monthly_min'};
					push @preferredtime,$config{$section}{'monthly_hour'};
					push @preferredtime,$config{$section}{'monthly_mday'};
					push @preferredtime,($datestamp{'mon'}-1); # january is month 0
					push @preferredtime,$datestamp{'year'};
					$lastpreferred = timelocal(@preferredtime);
					if ($lastpreferred > time()) { $lastpreferred -= 60*60*24*31; } # preferred time is later this month - so look at last month's
				} elsif ($type eq 'yearly')  {
					push @preferredtime,0; # try to hit 0 seconds
					push @preferredtime,$config{$section}{'yearly_min'};
					push @preferredtime,$config{$section}{'yearly_hour'};
					push @preferredtime,$config{$section}{'yearly_mday'};
					push @preferredtime,($config{$section}{'yearly_mon'}-1); # january is month 0
					push @preferredtime,$datestamp{'year'};
					$lastpreferred = timelocal(@preferredtime);
					if ($lastpreferred > time()) { $lastpreferred -= 60*60*24*31*365.25; } # preferred time is later this year - so look at last year
				} else {
					warn "WARN: unknown interval type $type in config!";
					next;
				}

				# reconstruct our human-formatted most recent preferred snapshot time into an epoch time, to compare with the epoch of our most recent snapshot
				my $maxage = time()-$lastpreferred;

				if ( $newestage > $maxage ) {
					# update to most current possible datestamp
					%datestamp = get_date();
					# print "we should have had a $type snapshot of $path $maxage seconds ago; most recent is $newestage seconds old.\n";

					my $flags = "";
					# use zfs (atomic) recursion if specified in config
					if ($config{$section}{'zfs_recursion'}) {
						$flags .= "r";
					}
					if ($handleDst) {
						$flags .= "d";
					}

					if ($flags ne "") {
						push(@newsnaps, "$path\@autosnap_$datestamp{'sortable'}_$type\@$flags");
					} else {
						push(@newsnaps, "$path\@autosnap_$datestamp{'sortable'}_$type");
					}
				}
			}
		}
	}

	if ( (scalar(@newsnaps)) > 0) {
		foreach my $snap ( @newsnaps ) {
			my $extraMessage = "";
			my @split = split '@', $snap, -1;
			my $recursiveFlag = 0;
			my $dstHandling = 0;
			if (scalar(@split) == 3) {
				my $flags = $split[2];
				if (index($flags, "r") != -1) {
					$recursiveFlag = 1;
					$extraMessage = " (zfs recursive)";
					chop $snap;
				}
				if (index($flags, "d") != -1) {
					$dstHandling = 1;
					chop $snap;
				}
				chop $snap;
			}
			my $dataset = $split[0];
			my $snapname = $split[1];
			my $presnapshotfailure = 0;
			my $ret = 0;
			if ($config{$dataset}{'pre_snapshot_script'}) {
				$ENV{'SANOID_TARGET'} = $dataset;
				$ENV{'SANOID_SNAPNAME'} = $snapname;
				if ($args{'verbose'}) { print "executing pre_snapshot_script '".$config{$dataset}{'pre_snapshot_script'}."' on dataset '$dataset'\n"; }

				if (!$args{'readonly'}) {
					$ret = runscript('pre_snapshot_script',$dataset);
				}

				delete $ENV{'SANOID_TARGET'};
				delete $ENV{'SANOID_SNAPNAME'};

				if ($ret != 0) {
					# warning was already thrown by runscript function
					$config{$dataset}{'no_inconsistent_snapshot'} and next;
					$presnapshotfailure = 1;
				}
			}
			if ($args{'verbose'}) { print "taking snapshot $snap$extraMessage\n"; }
			if (!$args{'readonly'}) {
				my $stderr;
				my $exit;
				($stderr, $exit) = tee_stderr {
					if ($recursiveFlag) {
						system($zfs, "snapshot", "-r", "$snap");
					} else {
						system($zfs, "snapshot", "$snap");
					}
				};

				$exit == 0 or do {
					if ($dstHandling) {
						if ($stderr =~ /already exists/) {
							$exit = 0;
							$snap =~ s/_([a-z]+)$/dst_$1/g;
							if ($args{'verbose'}) { print "taking dst snapshot $snap$extraMessage\n"; }
							if ($recursiveFlag) {
								system($zfs, "snapshot", "-r", "$snap") == 0
									or warn "CRITICAL ERROR: $zfs snapshot -r $snap failed, $?";
							} else {
								system($zfs, "snapshot", "$snap") == 0
									or warn "CRITICAL ERROR: $zfs snapshot $snap failed, $?";
							}
						}
					}
				};

				$exit == 0 or do {
					if ($recursiveFlag) {
						warn "CRITICAL ERROR: $zfs snapshot -r $snap failed, $?";
					} else {
						warn "CRITICAL ERROR: $zfs snapshot $snap failed, $?";
					}
				};
			}
			if ($config{$dataset}{'post_snapshot_script'}) {
				if (!$presnapshotfailure or $config{$dataset}{'force_post_snapshot_script'}) {
					$ENV{'SANOID_TARGET'} = $dataset;
					$ENV{'SANOID_SNAPNAME'} = $snapname;
					if ($args{'verbose'}) { print "executing post_snapshot_script '".$config{$dataset}{'post_snapshot_script'}."' on dataset '$dataset'\n"; }

					if (!$args{'readonly'}) {
						runscript('post_snapshot_script',$dataset);
					}

					delete $ENV{'SANOID_TARGET'};
					delete $ENV{'SANOID_SNAPNAME'};
				}
			}
		}
		$forcecacheupdate = 1;
		%snaps = getsnaps(%config,$cacheTTL,$forcecacheupdate);
	}
}

####################################################################################
####################################################################################
####################################################################################

sub blabber {

	my ($config, $snaps, $snapsbytype, $snapsbypath) = @_;

	$Data::Dumper::Sortkeys = 1;
	print "****** CONFIGS ******\n";
	print Dumper(\%config);
	#print "****** SNAPSHOTS ******\n";
	#print Dumper(\%snaps);
	#print "****** SNAPSBYTYPE ******\n";
	#print Dumper(\%snapsbytype);
	#print "****** SNAPSBYPATH ******\n";
	#print Dumper(\%snapsbypath);

	print "\n";

	foreach my $section (keys %config) {
		my $path = $config{$section}{'path'};
		print "Filesystem $path has:\n";
		print "     $snapsbypath{$path}{'numsnaps'} total snapshots ";
		if ($snapsbypath{$path}{'numsnaps'} == 0) {
			print "(no current snapshots)"
		} else {
			print "(newest: ";
			my $newest = sprintf("%.1f",$snapsbypath{$path}{'newest'} / 60 / 60);
			print "$newest hours old)\n";

			foreach my $type (keys %{ $snapsbytype{$path} }){
				print "          $snapsbytype{$path}{$type}{'numsnaps'} $type\n";
				print "              desired: $config{$section}{$type}\n";
				print "              newest: ";
				my $newest = sprintf("%.1f",($snapsbytype{$path}{$type}{'newest'} / 60 / 60));
				print "$newest hours old, named $snapsbytype{$path}{$type}{'newestname'}\n";
			}
		}
		print "\n\n";
	}

} # end blabber


####################################################################################
####################################################################################
####################################################################################


sub getsnapsbytype {

	my ($config, $snaps) = @_;
	my %snapsbytype;

	# iterate through each module section - each section is a single ZFS path
	foreach my $section (keys %config) {
		my $path = $config{$section}{'path'};

		my %rawsnaps;
		foreach my $name (keys %{ $snaps{$path} }){
			my $type = $snaps{$path}{$name}{'type'};
			$rawsnaps{$type}{$name} = $snaps{$path}{$name}{'ctime'}
		}

		# iterate through snapshots of each type, ordered by creation time of each snapshot within that type
		foreach my $type (keys %rawsnaps) {
			$snapsbytype{$path}{$type}{'numsnaps'} = scalar (keys %{ $rawsnaps{$type} });
			my @sortedsnaps;
			foreach my $name (
				sort { $rawsnaps{$type}{$a} <=> $rawsnaps{$type}{$b} } keys %{ $rawsnaps{$type} }
				) {
				push @sortedsnaps, $name;
				$snapsbytype{$path}{$type}{'newest'} = (time-$snaps{$path}{$name}{'ctime'});
				$snapsbytype{$path}{$type}{'newestname'} = $name;
			}
			$snapsbytype{$path}{$type}{'sorted'} = join ('|',@sortedsnaps);
		}
	}

	return %snapsbytype;

} # end getsnapsbytype


####################################################################################
####################################################################################
####################################################################################


sub getsnapsbypath {

	my ($config,$snaps) = @_;
	my %snapsbypath;

	# iterate through each module section - each section is a single ZFS path
	foreach my $section (keys %config) {
		my $path = $config{$section}{'path'};
		$snapsbypath{$path}{'numsnaps'} = scalar (keys %{ $snaps{$path} });

		# iterate through snapshots of each type, ordered by creation time of each snapshot within that type
		my %rawsnaps;
		foreach my $snapname ( keys %{ $snaps{$path} } ) {
			$rawsnaps{$path}{$snapname} = $snaps{$path}{$snapname}{'ctime'};
		}
		my @sortedsnaps;
		foreach my $snapname (
				sort { $rawsnaps{$path}{$a} <=> $rawsnaps{$path}{$b} } keys %{ $rawsnaps{$path} }
				) {
				push @sortedsnaps, $snapname;
				$snapsbypath{$path}{'newest'} = (time-$snaps{$path}{$snapname}{'ctime'});
		}
		my $sortedsnaps = join ('|',@sortedsnaps);
		$snapsbypath{$path}{'sorted'} = $sortedsnaps;
	}

	return %snapsbypath;

} # end getsnapsbypath




####################################################################################
####################################################################################
####################################################################################

sub getsnaps {

	my ($config, $cacheTTL, $forcecacheupdate) = @_;

	my @rawsnaps;

	my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($cache);

	if ( $forcecacheupdate || ! -f $cache || (time() - $mtime) > $cacheTTL ) {
		if (checklock('sanoid_cacheupdate')) {
			writelock('sanoid_cacheupdate');
			if ($args{'verbose'}) {
				if ($args{'force-update'}) {
					print "INFO: cache forcibly expired - updating from zfs list.\n";
				} else {
					print "INFO: cache expired - updating from zfs list.\n";
				}
			}
			open FH, "$zfs get -Hrpt snapshot creation |";
			@rawsnaps = <FH>;
			close FH;

			open FH, "> $cache" or die 'Could not write to $cache!\n';
			print FH @rawsnaps;
			close FH;
			removelock('sanoid_cacheupdate');
		} else {
			if ($args{'verbose'}) { print "INFO: deferring cache update - valid cache update lock held by another sanoid process.\n"; }
			open FH, "< $cache";
			@rawsnaps = <FH>;
			close FH;
		}
	} else {
		# if ($args{'debug'}) { print "DEBUG: cache not expired (" . (time() - $mtime) . " seconds old with TTL of $cacheTTL): pulling snapshot list from cache.\n"; }
		open FH, "< $cache";
		@rawsnaps = <FH>;
		close FH;
	}

	foreach my $snap (@rawsnaps) {
		my ($fs,$snapname,$snapdate) = ($snap =~ m/(.*)\@(.*ly)\t*creation\t*(\d*)/);

		# avoid pissing off use warnings
		if (defined $snapname) {
			my ($snaptype) = ($snapname =~ m/.*_(\w*ly)/);
			if ($snapname =~ /^autosnap/) {
				$snaps{$fs}{$snapname}{'ctime'}=$snapdate;
				$snaps{$fs}{$snapname}{'type'}=$snaptype;
			}
		}
	}

	return %snaps;
}

####################################################################################
####################################################################################
####################################################################################

sub init {
	my ($conf_file, $default_conf_file) = @_;
	my %config;

	unless (-e $default_conf_file ) { die "FATAL: cannot load $default_conf_file - please restore a clean copy, this is not a user-editable file!"; }
	unless (-e $conf_file ) { die "FATAL: cannot load $conf_file - please create a valid local config file before running sanoid!"; }

	tie my %defaults, 'Config::IniFiles', ( -file => $default_conf_file ) or die "FATAL: cannot load $default_conf_file - please restore a clean copy, this is not a user-editable file!";
	tie my %ini, 'Config::IniFiles', ( -file => $conf_file ) or die "FATAL: cannot load $conf_file - please create a valid local config file before running sanoid!";

	# we'll use these later to normalize potentially true and false values on any toggle keys
	my @toggles = ('autosnap','autoprune','monitor_dont_warn','monitor_dont_crit','monitor','recursive','process_children_only','skip_children','no_inconsistent_snapshot','force_post_snapshot_script');
	# recursive is defined as toggle but can also have the special value "zfs", it is kept to be backward compatible

	my @istrue=(1,"true","True","TRUE","yes","Yes","YES","on","On","ON");
	my @isfalse=(0,"false","False","FALSE","no","No","NO","off","Off","OFF");

	# check if default configuration file is up to date
	my $defaults_version = 1;
	if (defined $defaults{'version'}{'version'}) {
		$defaults_version = $defaults{'version'}{'version'};
		delete $defaults{'version'};
	}

	if ($defaults_version < $MINIMUM_DEFAULTS_VERSION) {
		die "FATAL: you're using sanoid.defaults.conf v$defaults_version, this version of sanoid requires a minimum sanoid.defaults.conf v$MINIMUM_DEFAULTS_VERSION";
	}

	foreach my $section (keys %ini) {

		# first up - die with honor if unknown parameters are set in any modules or templates by the user.
		foreach my $key (keys %{$ini{$section}}) {
			if (! defined ($defaults{'template_default'}{$key})) {
				die "FATAL ERROR: I don't understand the setting $key you've set in \[$section\] in $conf_file.\n";
			}
		}

		if ($section =~ /^template_/) { next; } # don't process templates directly

		# only set defaults on sections that haven't already been initialized - this allows us to override values
		# for sections directly when they've already been defined recursively, without starting them over from scratch.
		if (! defined ($config{$section}{'initialized'})) {
			if ($args{'debug'}) { print "DEBUG: initializing \$config\{$section\} with default values from $default_conf_file.\n"; }
			# set default values from %defaults, which can then be overriden by template
			# and/or local settings within the module.
			foreach my $key (keys %{$defaults{'template_default'}}) {
				if (! ($key =~ /template|recursive|children_only/)) {
					$config{$section}{$key} = $defaults{'template_default'}{$key};
				}
			}

			# override with values from user-defined default template, if any

			foreach my $key (keys %{$ini{'template_default'}}) {
				if ($key =~ /template|recursive/) {
					warn "ignored key '$key' from user-defined default template.\n";
					next;
				}
				if ($args{'debug'}) { print "DEBUG: overriding $key on $section with value from user-defined default template.\n"; }
				$config{$section}{$key} = $ini{'template_default'}{$key};
			}
		}

		# override with values from user-defined templates applied to this module,
		# in the order they were specified (ie use_template = default,production,mytemplate)
		if (defined $ini{$section}{'use_template'}) {
			my @templates = split (' *, *',$ini{$section}{'use_template'});
			foreach my $rawtemplate (@templates) {
				# strip trailing whitespace
				$rawtemplate =~ s/\s+$//g;

				my $template = 'template_'.$rawtemplate;
				foreach my $key (keys %{$ini{$template}}) {
					if ($key =~ /template|recursive/) {
						warn "ignored key '$key' from '$rawtemplate' template.\n";
						next;
					}
					if ($args{'debug'}) { print "DEBUG: overriding $key on $section with value from user-defined template $template.\n"; }
					$config{$section}{$key} = $ini{$template}{$key};
				}
			}
		}

		# override with any locally set values in the module itself
		foreach my $key (keys %{$ini{$section}} ) {
			if (! ($key =~ /template|recursive|skip_children/)) {
				if ($args{'debug'}) { print "DEBUG: overriding $key on $section with value directly set in module.\n"; }
				$config{$section}{$key} = $ini{$section}{$key};
			}
		}

		# make sure that true values are true and false values are false for any toggled values
		foreach my $toggle(@toggles) {
			foreach my $true (@istrue) {
				if (defined $config{$section}{$toggle} && $config{$section}{$toggle} eq $true) { $config{$section}{$toggle} = 1; }
			}
			foreach my $false (@isfalse) {
				if (defined $config{$section}{$toggle} && $config{$section}{$toggle} eq $false) { $config{$section}{$toggle} = 0; }
			}
		}

		# section path is the section name, unless section path has been explicitly defined
		if (defined ($ini{$section}{'path'})) {
			$config{$section}{'path'} = $ini{$section}{'path'};
		} else {
			$config{$section}{'path'} = $section;
		}

		# how 'bout some recursion? =)
		if ($config{$section}{'zfs_recursion'} && $config{$section}{'zfs_recursion'} == 1 && $config{$section}{'autosnap'} == 1) {
			warn "ignored autosnap configuration for '$section' because it's part of a zfs recursion.\n";
			$config{$section}{'autosnap'} = 0;
		}

		my $recursive = $ini{$section}{'recursive'} && grep( /^$ini{$section}{'recursive'}$/, @istrue );
		my $zfsRecursive = $ini{$section}{'recursive'} && $ini{$section}{'recursive'} =~ /zfs/i;
		my $skipChildren = $ini{$section}{'skip_children'} && grep( /^$ini{$section}{'skip_children'}$/, @istrue );
		my @datasets;
		if ($zfsRecursive || $recursive || $skipChildren) {
			if ($zfsRecursive) {
				$config{$section}{'zfs_recursion'} = 1;
			}

			@datasets = getchilddatasets($config{$section}{'path'});
			DATASETS: foreach my $dataset(@datasets) {
				chomp $dataset;

				if ($zfsRecursive) {
					# don't try to take the snapshot ourself, recursive zfs snapshot will take care of that
					$config{$dataset}{'autosnap'} = 0;

					foreach my $key (keys %{$config{$section}} ) {
						if (! ($key =~ /template|recursive|children_only|autosnap/)) {
							if ($args{'debug'}) { print "DEBUG: recursively setting $key from $section to $dataset.\n"; }
							$config{$dataset}{$key} = $config{$section}{$key};
						}
					}
				} else {
					if ($skipChildren) {
						if ($args{'debug'}) { print "DEBUG: ignoring $dataset.\n"; }
						delete $config{$dataset};
						next DATASETS;
					}

					foreach my $key (keys %{$config{$section}} ) {
						if (! ($key =~ /template|recursive|children_only/)) {
							if ($args{'debug'}) { print "DEBUG: recursively setting $key from $section to $dataset.\n"; }
							$config{$dataset}{$key} = $config{$section}{$key};
						}
					}
				}

				$config{$dataset}{'path'} = $dataset;
				$config{$dataset}{'initialized'} = 1;
			}
		}



	}

	return %config;
} # end sub init

####################################################################################
####################################################################################
####################################################################################

sub get_date {
	my %datestamp;
	($datestamp{'sec'},$datestamp{'min'},$datestamp{'hour'},$datestamp{'mday'},$datestamp{'mon'},$datestamp{'year'},$datestamp{'wday'},$datestamp{'yday'},$datestamp{'isdst'}) = localtime(time);
	$datestamp{'year'} += 1900;
	$datestamp{'unix_time'} = (((((((($datestamp{'year'} - 1971) * 365) + $datestamp{'yday'}) * 24) + $datestamp{'hour'}) * 60) + $datestamp{'min'}) * 60) + $datestamp{'sec'};
	$datestamp{'sec'} = sprintf ("%02u", $datestamp{'sec'});
	$datestamp{'min'} = sprintf ("%02u", $datestamp{'min'});
	$datestamp{'hour'} = sprintf ("%02u", $datestamp{'hour'});
	$datestamp{'mday'} = sprintf ("%02u", $datestamp{'mday'});
	$datestamp{'mon'} = sprintf ("%02u", ($datestamp{'mon'} + 1));
	$datestamp{'noseconds'} = "$datestamp{'year'}-$datestamp{'mon'}-$datestamp{'mday'}_$datestamp{'hour'}:$datestamp{'min'}";
	$datestamp{'sortable'} = "$datestamp{'noseconds'}:$datestamp{'sec'}";
	return %datestamp;
}

####################################################################################
####################################################################################
####################################################################################


sub displaytime {
        # take a time in seconds, return it in human readable form
        my ($elapsed) = @_;

        my $days = int ($elapsed / 60 / 60 / 24);
        $elapsed -= $days * 60 * 60 * 24;
        my $hours = int ($elapsed / 60 / 60);
        $elapsed -= $hours * 60 * 60;
        my $minutes = int ($elapsed / 60);
        $elapsed -= $minutes * 60;
        my $seconds = int($elapsed);
        my $humanreadable;
        if ($days) { $humanreadable .= " $days" . 'd'; }
        if ($hours || $days) { $humanreadable .= " $hours" . 'h'; }
        if ($minutes || $hours || $days) { $humanreadable .= " $minutes" . 'm'; }
        $humanreadable .= " $seconds" . 's';
        $humanreadable =~ s/^ //;
        return $humanreadable;
}


####################################################################################
####################################################################################
####################################################################################

sub check_zpool() {
	# check_zfs Nagios plugin for monitoring Sun ZFS zpools
	# Copyright (c) 2007
	# original Written by Nathan Butcher
	# adapted for use within Sanoid framework by Jim Salter (2014)
	#
	# Released under the GNU Public License
	#
	# This program is free software; you can redistribute it and/or modify
	# it under the terms of the GNU General Public License as published by
	# the Free Software Foundation; either version 2 of the License, or
	# (at your option) any later version.
	#
	# This program is distributed in the hope that it will be useful,
	# but WITHOUT ANY WARRANTY; without even the implied warranty of
	# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	# GNU General Public License for more details.
	#
	# You should have received a copy of the GNU General Public License
	# along with this program; if not, write to the Free Software
	# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	# Version: 0.9.2
	# Date : 24th July 2007
	# This plugin has tested on FreeBSD 7.0-CURRENT and Solaris 10
	# With a bit of fondling, it could be expanded to recognize other OSes in
	# future (e.g. if FUSE Linux gets off the ground)

	# Verbose levels:-
	# 1 - Only alert us of zpool health and size stats
	# 2 - ...also alert us of failed devices when things go bad
	# 3 - ...alert us of the status of all devices regardless of health
	#
	# Usage:   check_zfs <zpool> <verbose level 1-3>
	# Example: check_zfs zeepool 1
	#	ZPOOL zeedata : ONLINE {Size:3.97G Used:183K Avail:3.97G Cap:0%}


	my %ERRORS=('DEPENDENT'=>4,'UNKNOWN'=>3,'OK'=>0,'WARNING'=>1,'CRITICAL'=>2);
	my $state="UNKNOWN";
	my $msg="FAILURE";

	my $pool=shift;
	my $verbose=shift;

	my $size="";
	my $used="";
	my $avail="";
	my $cap="";
	my $health="";
	my $dmge="";
	my $dedup="";

	if ($verbose < 1 || $verbose > 3) {
		print "Verbose levels range from 1-3\n";
		exit $ERRORS{$state};
	}

	my $statcommand="$zpool list -o name,size,cap,health,free $pool";

	if (! open STAT, "$statcommand|") {
		print ("$state '$statcommand' command returns no result! NOTE: This plugin needs OS support for ZFS, and execution with root privileges.\n");
		exit $ERRORS{$state};
	}

	# chuck the header line
	my $header = <STAT>;

	# find and parse the line with values for the pool
	while(<STAT>) {
		chomp;
		if (/^${pool}\s+/) {
			my @row = split (/ +/);
			my $name;
			($name, $size, $cap, $health, $avail) = @row;
		}
	}

	# Tony: Debuging
	# print "Size: $size \t Used: $used \t Avai: $avail \t Cap: $cap \t Health: $health\n";

	close(STAT);

	## check for valid zpool list response from zpool
	if (! $health ) {
		$state = "CRITICAL";
		$msg = sprintf "ZPOOL {%s} does not exist and/or is not responding!\n", $pool;
		print $state, " ", $msg;
		exit ($ERRORS{$state});
	}

	## determine health of zpool and subsequent error status
	if ($health eq "ONLINE" ) {
		$state = "OK";
	} else {
		if ($health eq "DEGRADED") {
			$state = "WARNING";
		} else {
			$state = "CRITICAL";
		}
	}

	## get more detail on possible device failure
	## flag to detect section of zpool status involving our zpool
	my $poolfind=0;

	$statcommand="$zpool status $pool";
	if (! open STAT, "$statcommand|") {
		$state = 'CRITICAL';
		print ("$state '$statcommand' command returns no result! NOTE: This plugin needs OS support for ZFS, and execution with root privileges.\n");
		exit $ERRORS{$state};
	}

	## go through zfs status output to find zpool fses and devices
	while(<STAT>) {
		chomp;

		if (/^\s${pool}/ && $poolfind==1) {
			$poolfind=2;
			next;
		} elsif ( $poolfind==1 ) {
			$poolfind=0;
		}

		if (/NAME\s+STATE\s+READ\s+WRITE\s+CKSUM/) {
			$poolfind=1;
		}

		if ( /^$/ ) {
			$poolfind=0;
		}

		if ($poolfind == 2) {

			## special cases pertaining to full verbose
			if (/^\sspares/) {
				next unless $verbose == 3;
				$dmge=$dmge . "[SPARES]:- ";
				next;
			}
			if (/^\s{5}spare\s/) {
				next unless $verbose == 3;
				my ($sta) = /spare\s+(\S+)/;
				$dmge=$dmge . "[SPARE:${sta}]:- ";
				next;
			}
			if (/^\s{5}replacing\s/) {
				next unless $verbose == 3;
				my $perc;
				my ($sta) = /^\s+\S+\s+(\S+)/;
				if (/%/) {
					($perc) = /([0-9]+%)/;
				} else {
					$perc = "working";
				}
				$dmge=$dmge . "[REPLACING:${sta} (${perc})]:- ";
				next;
			}

			## other cases
			my ($dev, $sta, $read, $write, $cksum) = /^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/;

			if (!defined($sta)) {
				# cache and logs are special and don't have a status
				next;
			}

			## pool online, not degraded thanks to dead/corrupted disk
			if ($state eq "OK" && $sta eq "UNAVAIL") {
				$state="WARNING";

				## switching to verbose level 2 to explain weirdness
				if ($verbose == 1) {
					$verbose =2;
				}
			}

			## no display for verbose level 1
			next if ($verbose==1);
			## don't display working devices for verbose level 2
			if ($verbose==2 && ($state eq "OK" || $sta eq "ONLINE" || $sta eq "AVAIL" || $sta eq "INUSE")) {
				# check for io/checksum errors

				my @vdeverr = ();
				if ($read != 0) { push @vdeverr, "read" };
				if ($write != 0) { push @vdeverr, "write" };
				if ($cksum != 0) { push @vdeverr, "cksum" };

				if (scalar @vdeverr) {
					$dmge=$dmge . "(" . $dev . ":" . join(", ", @vdeverr) . " errors) ";
					if ($state eq "OK") { $state = "WARNING" };
				}

				next;
			}

			## show everything else
			if (/^\s{3}(\S+)/) {
				$dmge=$dmge . "<" . $dev . ":" . $sta . "> ";
			} elsif (/^\s{7}(\S+)/) {
				$dmge=$dmge . "(" . $dev . ":" . $sta . ") ";
			} else {
				$dmge=$dmge . $dev . ":" . $sta . " ";
			}
		}
	}

	## calling all goats!

	$msg = sprintf "ZPOOL %s : %s {Size:%s Free:%s Cap:%s} %s\n", $pool, $health, $size, $avail, $cap, $dmge;
	$msg = "$state $msg";
	return ($ERRORS{$state},$msg);
} # end check_zpool()

sub check_capacity_limit {
	my $value = shift;

	if (!defined($value) || $value !~ /^\d+\z/) {
		return undef;
	}

	if ($value < 0 || $value > 100) {
		return undef;
	}

	return 1
}

sub check_zpool_capacity() {
	my %ERRORS=('DEPENDENT'=>4,'UNKNOWN'=>3,'OK'=>0,'WARNING'=>1,'CRITICAL'=>2);
	my $state="UNKNOWN";
	my $msg="FAILURE";

	my $pool=shift;
	my $capacitylimitsref=shift;
	my %capacitylimits=%$capacitylimitsref;

	my $statcommand="$zpool list -H -o cap $pool";

	if (! open STAT, "$statcommand|") {
		print ("$state '$statcommand' command returns no result!\n");
		exit $ERRORS{$state};
	}

	my $line = <STAT>;
	close(STAT);

	chomp $line;
	my @row = split(/ +/, $line);
	my $cap=$row[0];

	## check for valid capacity value
	if ($cap !~ m/^[0-9]{1,3}%$/ ) {
		$state = "CRITICAL";
		$msg = sprintf "ZPOOL {%s} does not exist and/or is not responding!\n", $pool;
		print $state, " ", $msg;
		exit ($ERRORS{$state});
	}

	$state="OK";

	# check capacity
	my $capn = $cap;
	$capn =~ s/\D//g;

	if (defined($capacitylimits{"warn"})) {
		if ($capn >= $capacitylimits{"warn"}) {
			$state = "WARNING";
		}
	}

	if (defined($capacitylimits{"crit"})) {
		if ($capn >= $capacitylimits{"crit"}) {
			$state = "CRITICAL";
		}
	}

	$msg = sprintf "ZPOOL %s : %s\n", $pool, $cap;
	$msg = "$state $msg";
	return ($ERRORS{$state},$msg);
} # end check_zpool_capacity()

sub check_prune_defer {
	my ($config, $section) = @_;

	my $limit = $config{$section}{"prune_defer"};

	if (!check_capacity_limit($limit)) {
		die "ERROR: invalid prune_defer limit!\n";
	}

	if ($limit eq 0) {
		return 0;
	}

	my @parts = split /\//, $section, 2;
	my $pool = $parts[0];

	if (exists $capacitycache{$pool}) {
	} else {
		$capacitycache{$pool} = get_zpool_capacity($pool);
	}

	if ($limit < $capacitycache{$pool}) {
		return 0;
	}

	return 1;
}

sub get_zpool_capacity {
	my $pool = shift;

	my $statcommand="$zpool list -H -o cap $pool";

	if (! open STAT, "$statcommand|") {
		die "ERROR: '$statcommand' command returns no result!\n";
	}

	my $line = <STAT>;
	close(STAT);

	chomp $line;
	my @row = split(/ +/, $line);
	my $cap=$row[0];

	## check for valid capacity value
	if ($cap !~ m/^[0-9]{1,3}%$/ ) {
		die "ERROR: '$statcommand' command returned invalid capacity value ($cap)!\n";
	}

	$cap =~ s/\D//g;

	return $cap;
}

######################################################################################################
######################################################################################################
######################################################################################################
######################################################################################################
######################################################################################################

sub checklock {
	# take argument $lockname.
	#
	# read /var/run/$lockname.lock for a pid on first line and a mutex on second line.
	#
	# check process list to see if the pid from /var/run/$lockname.lock is still active with
	# the original mutex found in /var/run/$lockname.lock.
	#
	# return:
	#    0 if lock is present and valid for another process
	#    1 if no lock is present
	#    2 if lock is present, but we own the lock
	#
	# shorthand - any true return indicates we are clear to lock; a false return indicates
	#             that somebody else already has the lock and therefore we cannot.
	#

	my $lockname = shift;
	my $lockfile = "/var/run/$lockname.lock";

	if (! -e $lockfile) {
		# no lockfile
		return 1;
	}
	# make sure lockfile contains something
	if ( -z $lockfile) {
	        # zero size lockfile, something is wrong
	        die "ERROR: something is wrong! $lockfile is empty\n";
	}

	# lockfile exists. read pid and mutex from it. see if it's our pid.  if not, see if
	# there's still a process running with that pid and with the same mutex.

	open FH, "< $lockfile" or die "ERROR: unable to open $lockfile";
	my @lock = <FH>;
	close FH;
	# if we didn't get exactly 2 items from the lock file there is a problem
	if (scalar(@lock) != 2) {
	    die "ERROR: $lockfile is invalid.\n"
	}

	my $lockmutex = pop(@lock);
	my $lockpid = pop(@lock);

	chomp $lockmutex;
	chomp $lockpid;

	if ($lockpid == $$) {
		# we own the lockfile. no need to check any further.
		return 2;
	}
	open PL, "$pscmd -p $lockpid -o args= |";
	my @processlist = <PL>;
	close PL;

	my $checkmutex = pop(@processlist);
	chomp $checkmutex;

	if ($checkmutex eq $lockmutex) {
		# lock exists, is valid, is not owned by us - return false
		return 0;
	} else {
		# lock is present but not valid - remove and return true
		unlink $lockfile;
		return 1;
	}
}

sub removelock {
	# take argument $lockname.
	#
	# make sure /var/run/$lockname.lock actually belongs to me (contains my pid and mutex)
	# and remove it if it does, die if it doesn't.

	my $lockname = shift;
	my $lockfile = "/var/run/$lockname.lock";

	if (checklock($lockname) == 2) {
		unlink $lockfile;
		return;
	} elsif (checklock($lockname) == 1) {
		die "ERROR: No valid lockfile found - Did a rogue process or user update or delete it?\n";
	} else {
		die "ERROR: A valid lockfile exists but does not belong to me! I refuse to remove it.\n";
	}
}

sub writelock {
	# take argument $lockname.
	#
	# write a lockfile to /var/run/$lockname.lock with first line
	# being my pid and second line being my mutex.

	my $lockname = shift;
	my $lockfile = "/var/run/$lockname.lock";

	# die honorably rather than overwriting a valid, existing lock
	if (! checklock($lockname)) {
		die "ERROR: Valid lock already exists - I refuse to overwrite it. Committing seppuku now.\n";
	}

	my $pid = $$;

	open PL, "$pscmd -p $$ -o args= |";
	my @processlist = <PL>;
	close PL;

	my $mutex = pop(@processlist);
	chomp $mutex;

	open FH, "> $lockfile";
	print FH "$pid\n";
	print FH "$mutex\n";
	close FH;
}

sub iszfsbusy {
	# check to see if ZFS filesystem passed in as argument currently has a zfs send or zfs receive process referencing it.
	# return true if busy (currently being sent or received), return false if not.

        my $fs = shift;
        # if (args{'debug'}) { print "DEBUG: checking to see if $fs on is already in zfs receive using $pscmd -Ao args= ...\n"; }

        open PL, "$pscmd -Ao args= |";
        my @processes = <PL>;
        close PL;

        foreach my $process (@processes) {
		# if ($args{'debug'}) { print "DEBUG: checking process $process...\n"; }
                if ($process =~ /zfs *(send|receive|recv).*$fs/) {
                        # there's already a zfs send/receive process for our target filesystem - return true
                        # if ($args{'debug'}) { print "DEBUG: process $process matches target $fs!\n"; }
                        return 1;
                }
        }

        # no zfs receive processes for our target filesystem found - return false
        return 0;
}

#######################################################################################################################3
#######################################################################################################################3
#######################################################################################################################3

sub getchilddatasets {
	# for later, if we make sanoid itself support sudo use
	my $fs = shift;
	my $mysudocmd = '';

	my $getchildrencmd = "$mysudocmd $zfs list -o name -t filesystem,volume -Hr $fs |";
	 if ($args{'debug'}) { print "DEBUG: getting list of child datasets on $fs using $getchildrencmd...\n"; }
	open FH, $getchildrencmd;
	my @children = <FH>;
	close FH;

	# parent dataset is the first element
	shift @children;

	return @children;
}

#######################################################################################################################3
#######################################################################################################################3
#######################################################################################################################3

sub removecachedsnapshots {
	my $wait = shift;

	if (not %pruned) {
		return;
	}

	my $unlocked = checklock('sanoid_cacheupdate');

	if ($wait != 1 && not $unlocked) {
		if ($args{'verbose'}) { print "INFO: deferring cache update (snapshot removal) - valid cache update lock held by another sanoid process.\n"; }
		return;
	}

	# wait until we can get a lock to do our cache changes
	while (not $unlocked) {
		if ($args{'verbose'}) { print "INFO: waiting for cache update lock held by another sanoid process.\n"; }
		sleep(10);
		$unlocked = checklock('sanoid_cacheupdate');
	}

	writelock('sanoid_cacheupdate');

	if ($args{'verbose'}) {
		print "INFO: removing destroyed snapshots from cache.\n";
	}
	open FH, "< $cache";
	my @rawsnaps = <FH>;
	close FH;

	open FH, "> $cache" or die 'Could not write to $cache!\n';
	foreach my $snapline ( @rawsnaps ) {
		my @columns = split("\t", $snapline);
		my $snap = $columns[0];
		print FH $snapline unless ( exists($pruned{$snap}) );
	}
	close FH;

	removelock('sanoid_cacheupdate');
	%snaps = getsnaps(\%config,$cacheTTL,$forcecacheupdate);

	# clear hash
	undef %pruned;
}

#######################################################################################################################3
#######################################################################################################################3
#######################################################################################################################3

sub runscript {
	my $key=shift;
	my $dataset=shift;

	my $timeout=$config{$dataset}{'script_timeout'};

	my $ret;
	eval {
		if ($timeout gt 0) {
			local $SIG{ALRM} = sub { die "alarm\n" };
			alarm $timeout;
		}
		$ret = system($config{$dataset}{$key});
		alarm 0;
	};
	if ($@) {
		if ($@ eq "alarm\n") {
			warn "WARN: $key didn't finish in the allowed time!";
		} else {
			warn "CRITICAL ERROR: $@";
		}
		return -1;
	} else {
		if ($ret != 0) {
			warn "WARN: $key failed, $?";
		}
	}

	return $ret;
}

#######################################################################################################################3
#######################################################################################################################3
#######################################################################################################################3

sub convertTimePeriod {
	my $value=shift;
	my $period=shift;

	if ($value =~ /^\d+[yY]$/) {
		$period = 60*60*24*31*365;
		chop $value;
	} elsif ($value =~ /^\d+[wW]$/) {
		$period = 60*60*24*7;
		chop $value;
	} elsif ($value =~ /^\d+[dD]$/) {
		$period = 60*60*24;
		chop $value;
	} elsif ($value =~ /^\d+[hH]$/) {
		$period = 60*60;
		chop $value;
	} elsif ($value =~ /^\d+[mM]$/) {
		$period = 60;
		chop $value;
	} elsif ($value =~ /^\d+[sS]$/) {
		$period = 1;
		chop $value;
	} elsif ($value =~ /^\d+$/) {
		# no unit, provided fallback period is used
	} else {
		# invalid value, return smallest valid value as fallback
		# (will trigger a warning message for monitoring for sure)
		return 1;
	}

	return $value * $period;
}

__END__

=head1 NAME

sanoid - ZFS snapshot management and replication tool

=head1 SYNOPSIS

sanoid [options]

Assumes --cron --verbose if no other arguments (other than configdir) are specified

Options:

  --configdir=DIR       Specify a directory to find config file sanoid.conf

  --cron                Creates snapshots and purges expired snapshots
  --verbose             Prints out additional information during a sanoid run
  --readonly            Simulates creation/deletion of snapshots
  --quiet               Suppresses non-error output
  --force-update        Clears out sanoid's zfs snapshot cache

  --monitor-health      Reports on zpool "health", in a Nagios compatible format
  --monitor-capacity    Reports on zpool capacity, in a Nagios compatible format
  --monitor-snapshots   Reports on snapshot "health", in a Nagios compatible format
  --take-snapshots      Creates snapshots as specified in sanoid.conf
  --prune-snapshots     Purges expired snapshots as specified in sanoid.conf
  --force-prune         Purges expired snapshots even if a send/recv is in progress

  --help                Prints this helptext
  --version             Prints the version number
  --debug               Prints out a lot of additional information during a sanoid run