[20180608]asmcmd顯示檔案的日期時間問題

lfree發表於2018-06-08

[20180608]asmcmd顯示檔案的日期時間問題.txt

http://blog.iarsov.com/oracle/date-and-time-format-in-asmcmd-for-file-listing/

--//如果你使用asmcmd檢視asm檔案,你可以發現日期時間的顯示格式:
SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production


ASMCMD [+data/fyhis/PARAMETERFILE] > ls -l
Type           Redund  Striped  Time             Sys  Name
PARAMETERFILE  UNPROT  COARSE   JUN 08 00:00:00  Y    spfile.272.931438543

--//僅僅顯示日期,而時分秒都是0.

ASMCMD [+data/fyhis/ARCHIVELOG/2017_02_09] > ls -ls thread_1_seq_3773.3605.935537807
Type        Redund  Striped  Time             Sys  Block_Size  Blocks   Bytes    Space  Name
ARCHIVELOG  UNPROT  COARSE   FEB 09     2017  Y           512     937  479744  1048576  thread_1_seq_3773.3605.935537807

--//而實際上asmcmd是shell 指令碼,最後執行的是perl指令碼.
$ file $(which asmcmd)
/u01/app/11.2.0/grid/bin/asmcmd: POSIX shell script text executable

--//連結:http://blog.iarsov.com/oracle/date-and-time-format-in-asmcmd-for-file-listing/
ASMCMD utility base module is asmcmdbase.pm

The subroutine for listing files located in the base module is asmcmdbase_ls_process_file. You can see the logic how
file dates are printed. We can notice that if the file is older than 6 months the format used is "MON DD YYYY". If the
file is newer, the format used is "MON DD HH24:MI:SS". This subroutine is called (executed) for each file that needs to
be printed in ASMCMD.

List entries $entry_info_ref->{'mod_date'} and $entry_info_ref->{'mod_time'} contain the data for both hardcoded
formats. MOD_DATE if the files is older than 6 months and MOD_TIME if the file is newer.

sub asmcmdbase_ls_process_file
{
my ($dbh, $entry_info_ref, $args_ref, $cur_date, $file_info) = @_;

my ($related_alias); # The user or system alias for its corresponding #
# system or user alias, respectively. #

# Calculate dates only if we have file info from v$asm_file.
if ($file_info)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
# Use separate date format if date older than half a year.
if (($cur_date - $entry_info_ref->{'julian_date'}) >= $ASMCMDBASE_SIXMONTH)
{ # If older than six months, use 'MON DD YYYY' format. #
$entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_date'};
}
else
{ # Otherwise, use 'MON DD HH24:MI:SS' format. #
$entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time'};
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
}

--//說明:如果舊於6個月,顯示日期.use 'MON DD YYYY' format.其他使用use 'MON DD HH24:MI:SS' format. #.
--//實際上這種模式與os的ls命令一樣,我個人非常不習慣這種風格,我喜歡格式'YYYY/MM/DD HH24:MI:SS',這樣國人的顯示風格一直.

$ locate asmcmdbase.pm
/u01/app/11.2.0/grid/lib/asmcmdbase.pm
/u01/app/oracle/product/11.2.0/dbhome_1/lib/asmcmdbase.pm

--//檢索sub asmcmdbase_ls_process_file,就可以定位以上資訊.
--//按照連結的提示修改,注意修改前一定要做好備份.
# cp asmcmdbase.pm asmcmdbase.pm.ORG
# cp asmcmdshare.pm asmcmdshare.pm.ORG

--//修改asmcmdshare.pm檔案:
# diff -Nur asmcmdshare.pm.ORG asmcmdshare.pm
--- asmcmdshare.pm.ORG  2018-06-08 09:00:56.566081223 +0800
+++ asmcmdshare.pm      2018-06-08 09:05:26.920081038 +0800
@@ -1916,11 +1916,14 @@
   my ($sth, $qry, $row, $cmpd_ind);

   $cmpd_ind = $gnum * (1 << 24) + $fnum;
+
+  if(!$ENV{'NLS_DATE_FORMAT_XX'}){ $ENV{'NLS_DATE_FORMAT_XX'} = "yyyy/mm/dd hh24:mi:ss"; }

   $qry = 'select group_number, file_number, incarnation, block_size, ' .
          'blocks, bytes, space, type, redundancy, striped, creation_date, ' .
          'user_number, usergroup_number, permissions, ' .
          'to_char(modification_date, \'MON DD HH24:MI:SS\') "MOD_TIME", ' .
+         'to_char(modification_date, \''.$ENV{'NLS_DATE_FORMAT_XX'}.'\') "MOD_TIME_XX", ' .
          'to_char(modification_date, \'MON DD YYYY\') "MOD_DATE", ' .
          'to_char(modification_date, \'J HH24 MI SS\') "JULIAN_TIME", ' .
          'to_char(modification_date, \'J\') "JULIAN_DATE" ' .
@@ -1945,6 +1948,7 @@
   $file_info_ref->{'creation_date'} = $row->{'CREATION_DATE'};
   $file_info_ref->{'mod_time'} = $row->{'MOD_TIME'};
   $file_info_ref->{'mod_date'} = $row->{'MOD_DATE'};
+  $file_info_ref->{'mod_time_xx'} = $row->{'MOD_TIME_XX'};
   $file_info_ref->{'julian_time'} = $row->{'JULIAN_TIME'};
   $file_info_ref->{'julian_date'} = $row->{'JULIAN_DATE'};
   $file_info_ref->{'user_number'} = $row->{'USER_NUMBER'};

--//修改asmcmdbase.pm檔案:
# diff -Nur asmcmdbase.pm.ORG asmcmdbase.pm
--- asmcmdbase.pm.ORG   2018-06-08 08:59:26.442748126 +0800
+++ asmcmdbase.pm       2018-06-08 09:08:48.659080899 +0800
@@ -2044,6 +2044,8 @@
     {                           # Otherwise, use 'MON DD HH24:MI:SS' format. #
       $entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time'};
     }
+       # Overwrite date_print
+       $entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time_xx'};
   }

   # Find system alias only if we have info from v$asm_file, i.e., if we're

--//定義環境變數:
$ export NLS_DATE_FORMAT_XX="yyyy/mm/dd hh24:mi:ss"

ASMCMD [+Data/Fyhis/PARAMETERFILE] > ls -l
Type           Redund  Striped  Time                 Sys  Name
Use of uninitialized value $print_args[5] in printf at /u01/app/11.2.0/grid/lib/asmcmdbase.pm line 2369, <STDIN> line 7.
PARAMETERFILE  UNPROT  COARSE   2018/06/08 00:00:00               Y    spfile.272.931438543

ASMCMD [+data/fyhis/ARCHIVELOG/2017_02_09] > ls -ls thread_1_seq_3773.3605.935537807
Type        Redund  Striped  Time                 Sys  Block_Size  Blocks   Bytes    Space  Name
Use of uninitialized value $print_args[5] in printf at /u01/app/11.2.0/grid/lib/asmcmdbase.pm line 2369, <STDIN> line 14.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ARCHIVELOG  UNPROT  COARSE   2017/02/09 23:00:00               Y           512     937  479744  1048576  thread_1_seq_3773.3605.935537807

--//有錯.
--//我仔細看了一下指令碼:
# vim asmcmdbase.pm
..
..

2306   if ($entry_ref->{'alias_directory'} eq 'Y')
2307   {                                        # Directries have the suffix '/'. #
2308     $entry_ref->{'name_print'} .= '/';
2309   }
2310   elsif (defined ($args_ref->{'l'}))
2311   {                                     # Date values exists for files only. #
2312     ($mon, $day, $time) = split (/\s+/, $entry_ref->{'date_print'});
~~~~~~~~//這裡要拆分3個引數,必須修改如下,這樣就存在3個引數:
+  if(!$ENV{'NLS_DATE_FORMAT_XX'}){ $ENV{'NLS_DATE_FORMAT_XX'} = "yyyy/mm/dd hh24:mi:ss "; }
~~~~~~~~~//注意我在ss後面加入1個空格,當然對應的不是月,日,時間.
2313   }
2314
2315
2316
2317   if (($entry_ref->{'alias_directory'} eq 'Y') ||
2318       (($entry_ref->{'system_created'} eq 'N') &&
2319        (!defined ($args_ref->{'L'}))))
2320   {
2321     $entry_ref->{'type'}       = $ASMCMDBASE_SPACE;
2322     $entry_ref->{'redundancy'} = $ASMCMDBASE_SPACE;
2323     $entry_ref->{'striped'}    = $ASMCMDBASE_SPACE;
2324     $mon                       = $ASMCMDBASE_SPACE;
2325     $day                       = $ASMCMDBASE_SPACE;
2326     $time                      = $ASMCMDBASE_SPACE;
2327     $entry_ref->{'space'}      = $ASMCMDBASE_SPACE;
2328
2329     $entry_ref->{'block_size'} = $ASMCMDBASE_SPACE;
2330     $entry_ref->{'blocks'}     = $ASMCMDBASE_SPACE;
2331     $entry_ref->{'bytes'}      = $ASMCMDBASE_SPACE;
2332
2333     $entry_ref->{'user'}       = $ASMCMDBASE_SPACE;
2334     $entry_ref->{'group'}      = $ASMCMDBASE_SPACE;
2335     $entry_ref->{'perm'}       = $ASMCMDBASE_SPACE;
2336   }
2337
2338   @print_args = ();
2339
2340   if (defined($args_ref->{'l'}))                                    # ls -l. #
2341   {
2342     push (@print_args, $entry_ref->{'type'});
2343     push (@print_args, $entry_ref->{'redundancy'});
2344     push (@print_args, $entry_ref->{'striped'});
2345     push (@print_args, $mon);
2346     push (@print_args, $day);
2347     push (@print_args, $time);
2348     push (@print_args, $ASMCMDBASE_SPACE);
2349     push (@print_args, $entry_ref->{'system_created'});
2350   }
2351
2352   if (defined($args_ref->{'s'}))                                    # ls -s. #
2353   {
2354     push (@print_args, $entry_ref->{'block_size'});
2355     push (@print_args, $entry_ref->{'blocks'});
2356     push (@print_args, $entry_ref->{'bytes'});
2357     push (@print_args, $entry_ref->{'space'});
2358   }
2359
2360   if (defined($args_ref->{'permission'}))                # ls --permission -l. #
2361   {
2362     push (@print_args, $entry_ref->{'user'});
2363     push (@print_args, $entry_ref->{'group'});
2364     push (@print_args, $entry_ref->{'perm'});
2365   }
2366
2367   push(@print_args, $entry_ref->{'name_print'});
2368
2369   printf ($row, @print_args);
2370
2371   return;
2372 }


--//修改後在執行OK.不再報錯.也許作者修改的版本與我的不同.

ASMCMD [+] > ls  +DATA/FYHIS/PARAMETERFILE -ls
Type           Redund  Striped  Time                 Sys  Block_Size  Blocks  Bytes    Space  Name
PARAMETERFILE  UNPROT  COARSE   2018/06/08 00:00:00      Y           512      13   6656  1048576  spfile.272.931438543

ASMCMD [+] > ls  +DATA/FYHIS/PARAMETERFILE -l
Type           Redund  Striped  Time                 Sys  Name
PARAMETERFILE  UNPROT  COARSE   2018/06/08 00:00:00      Y    spfile.272.931438543

--//還有1個缺點就是sys,name列出現偏移.
--//當然也一些變通的方法,比如定義環境變數:
export NLS_DATE_FORMAT_XX="YY/MM/DD HH24 "
--//注意引號後有1個空格,這樣就認為有3個欄位.

ASMCMD [+] > ls  +DATA/FYHIS/PARAMETERFILE -l
Type           Redund  Striped  Time             Sys  Name
PARAMETERFILE  UNPROT  COARSE   18/06/08 00           Y    spfile.272.931438543

--//但是最後2列還是存在偏移.

--//有點亂,最後修改如下:
# diff -Nur asmcmdshare.pm.ORG asmcmdshare.pm
--- asmcmdshare.pm.ORG  2018-06-08 09:00:56.566081223 +0800
+++ asmcmdshare.pm      2018-06-08 10:14:21.633059695 +0800
@@ -1916,11 +1916,14 @@
   my ($sth, $qry, $row, $cmpd_ind);

   $cmpd_ind = $gnum * (1 << 24) + $fnum;
+
+  if(!$ENV{'NLS_DATE_FORMAT_XX'}){ $ENV{'NLS_DATE_FORMAT_XX'} = "YYYY/MM/DD HH24:MI:SS "; }

   $qry = 'select group_number, file_number, incarnation, block_size, ' .
          'blocks, bytes, space, type, redundancy, striped, creation_date, ' .
          'user_number, usergroup_number, permissions, ' .
          'to_char(modification_date, \'MON DD HH24:MI:SS\') "MOD_TIME", ' .
+         'to_char(modification_date, \''.$ENV{'NLS_DATE_FORMAT_XX'}.'\') "MOD_TIME_XX", ' .
          'to_char(modification_date, \'MON DD YYYY\') "MOD_DATE", ' .
          'to_char(modification_date, \'J HH24 MI SS\') "JULIAN_TIME", ' .
          'to_char(modification_date, \'J\') "JULIAN_DATE" ' .
@@ -1945,6 +1948,7 @@
   $file_info_ref->{'creation_date'} = $row->{'CREATION_DATE'};
   $file_info_ref->{'mod_time'} = $row->{'MOD_TIME'};
   $file_info_ref->{'mod_date'} = $row->{'MOD_DATE'};
+  $file_info_ref->{'mod_time_xx'} = $row->{'MOD_TIME_XX'};
   $file_info_ref->{'julian_time'} = $row->{'JULIAN_TIME'};
   $file_info_ref->{'julian_date'} = $row->{'JULIAN_DATE'};
   $file_info_ref->{'user_number'} = $row->{'USER_NUMBER'};

# diff -Nur asmcmdbase.pm.ORG asmcmdbase.pm
--- asmcmdbase.pm.ORG   2018-06-08 08:59:26.442748126 +0800
+++ asmcmdbase.pm       2018-06-08 10:12:15.591059782 +0800
@@ -2044,6 +2044,8 @@
     {                           # Otherwise, use 'MON DD HH24:MI:SS' format. #
       $entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time'};
     }
+       # Overwrite date_print
+       $entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time_xx'};
   }

   # Find system alias only if we have info from v$asm_file, i.e., if we're

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2155826/,如需轉載,請註明出處,否則將追究法律責任。

相關文章