Package: sysstat / 11.4.3-2

12-isag-new-format-support.patch Patch series | 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
From: Robert Luberda <robert@debian.org>
Date: Fri, 29 Aug 2014 08:31:41 +0200
Subject: 12-isag-new-format-support

Add support for the new `saYYYYMMDD' log files format to isag.

The fill_file_menu() function was rewritten and tries to handle
all three possible formats (`saYYYYMMDD, `saDD', `YYYYMM/saDD')
by collecting existing log files into a temporary list of strings,
which is then used after being sorted to generate the menu.

Not to make the menu too long the `saYYYYMMDD' files are displayed
as `DD' files in `saYYYYMM' submenus.
---
 contrib/isag/isag.in | 109 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 59 insertions(+), 50 deletions(-)

diff --git a/contrib/isag/isag.in b/contrib/isag/isag.in
index 6bf16ac..9011fae 100644
--- a/contrib/isag/isag.in
+++ b/contrib/isag/isag.in
@@ -275,7 +275,7 @@ proc out_msg {title text} {
 
 # specific paths and programs
 set sar_data_path "@SA_DIR@"
-set sar_data_mask "sa\[0-9\]\[0-9\]"
+set sar_data_mask "sa\[0-9\]{\[0-9\],\[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]}"
 set prefix "@prefix@"
 set exec_prefix "@exec_prefix@"
 set sar "@bindir@/sar.sysstat"
@@ -527,68 +527,77 @@ proc fill_file_menu {} {
 	set directories [lsort -decreasing [glob -nocomplain -directory $sar_data_path -type d \[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]]]
 
 	menu .file.menu.m -tearoff false
+	set ldirectory [string length $sar_data_path]
+	# List of strings in format "directory:mtime:menu_item_label:real_file_name" created from log file names
+	set tmpfiles [list]
 
-	foreach directory [concat $directories $sar_data_path ] {		
+	foreach directory [concat $directories $sar_data_path ] {
 		set files [glob -nocomplain -directory $directory -type f $sar_data_mask ]
 		# add compressed files too
 		set files [concat $files [glob -nocomplain -directory $directory -type f $sar_data_mask.gz ]]
 		set files [concat $files [glob -nocomplain -directory $directory -type f $sar_data_mask.bz2 ]]
+		# remove $sar_data_path prefix from $directory
+		set directory [string range $directory [expr $ldirectory + 1] end]
 
-		if { 0 == [llength $files] } {
-			continue
+		foreach file $files {
+			# skip symbolic links
+			if { "link" == [ file type $file]} { continue }
+
+			# remove $sar_data_path prefix from $file
+			set file [string range $file [expr $ldirectory + 1] end]
+
+			if { $directory == ""} {
+				set l [string length $file]
+				if { $l < 10 } {
+					# old file format: saDD
+					set timestamp [file mtime "$sar_data_path/$file"]
+					set tmpfiles [linsert $tmpfiles end ".:$timestamp:$file:$file"]
+				} else {
+					# new file format: saYYYYMMDD
+					set dir [string range $file 0 7]
+					set basefile [string range $file 8 end]
+					set tmpfiles [linsert $tmpfiles end "$dir:0:$basefile:$file"]
+				}
+			} else {
+				# old file format: YYYYMM/saDD
+				set l [string last "/" $file]
+				set basefile [string range $file [expr $l + 1] end]
+				set tmpfiles [linsert $tmpfiles end "$directory:0:$basefile:$file"]
+			}
 		}
+	}
 
-		if { $directory == $sar_data_path } {
-			set menudir ".file.menu.m"
-			
-			# Sort files according to theirs modification times descending
-
-			# i) create temporary list of items in form `timestamp:file'
-			set tmpfiles [list]
-			foreach file $files {
-				# skip symbolic links
-				if { "link" == [ file type $file]} { continue }
-				set timestamp [file mtime $file]
-				set tmpfiles [linsert $tmpfiles end "$timestamp:$file"]
-			}
-			
-			# ii) sort the temporary list 
-			set tmpfiles [lsort -decreasing $tmpfiles]
-			# iii) remove timestamps to get back the file list
-			set files [list]
-			foreach file $tmpfiles {
-				set l [string first ":" $file]
-				set files [linsert $files end [string range $file [expr $l+1] end]]
+	# Sort the list
+	set tmpfiles [lsort -decreasing $tmpfiles]
+	set prevdir ""
+
+	# Fill menu with files
+	foreach file $tmpfiles {
+		set components [split $file ":"]
+		lassign $components dir timestamp label basefile
+		if { $prevdir != $dir } {
+			set prevdir $dir
+			if { $dir == "." } {
+				set menudir ".file.menu.m"
+			} else {
+				set menudir ".file.menu.m.dir$dir"
+				menu $menudir -tearoff 0
+				.file.menu.m add cascade -label $dir -menu $menudir
 			}
-		} else {
-			# sort file list by file name, descending 
-			set files [ lsort -decreasing $files ]
-			set l [string last "/" $directory]
-			set dir [string range  $directory  [expr $l + 1] end]
-
-			# create submenu for the $dir directory
-			set menudir ".file.menu.m.dir$dir"
-			menu $menudir -tearoff 0
-			.file.menu.m add cascade -label $dir -menu $menudir
 		}
 
+		set label [string map { .gz "" } $label]
+		set label [string map { .bz2 "" } $label]
+		$menudir add command -label "$label" -command "set_file $basefile"
 
-		# Fill menu with files
-		foreach file $files {
-			set l [string length $sar_data_path]
-			set basefile [string range $file [expr $l + 1] end]
-			set l [string last "/" $basefile]
-			set label [string range  $basefile  [expr $l + 1] end]
-			set label [string map { .gz "" } $label]
-			set label [string map { .bz2 "" } $label]
-
-			$menudir add command -label "$label" -command "set_file $basefile"
+		set file "$sar_data_path/$basefile"
+		if { $timestamp == 0 } {
 			set timestamp [file mtime $file]
-			# ignore files small then 4kB to avoid "No data found" error
-			if { ($timestamp > $max_timestamp) && ([file size $file] > 4096) } {
-				set sag_latest_file  $basefile
-				set max_timestamp  $timestamp
-			}			
+		}
+		# Ignore files smaller then 4kB to avoid "No data found" error
+		if { ($timestamp > $max_timestamp) && ([file size $file] > 4096) } {
+			set sag_latest_file  $basefile
+			set max_timestamp  $timestamp
 		}
 	}
 	# Add Refresh button to fix https://bugs.launchpad.net/ubuntu/+source/sysstat/+bug/924197