File: logtail.rb

package info (click to toggle)
ruby-rbvmomi 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,756 kB
  • sloc: ruby: 5,590; sh: 36; makefile: 7
file content (60 lines) | stat: -rw-r--r-- 1,276 bytes parent folder | download | duplicates (3)
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
# Translation of example 2-2 from the vSphere SDK for Perl Programming Guide
require 'trollop'
require 'rbvmomi'
require 'rbvmomi/trollop'

VIM = RbVmomi::VIM

opts = Trollop.options do
  banner <<-EOS
Follow a log file.

Usage:
    logtail.rb [options] [logKey]

If logKey is not provided the list of available log keys will be printed and
the program will exit.

VIM connection options:
    EOS

    rbvmomi_connection_opts

    text <<-EOS

Other options:
  EOS
end

Trollop.die("must specify host") unless opts[:host]
logKey = ARGV[0]

vim = VIM.connect opts
diagMgr = vim.serviceContent.diagnosticManager

if not logKey
  puts "Available logs:"
  diagMgr.QueryDescriptions.each do |desc|
    puts "#{desc.key}: #{desc.info.label}"
  end
  exit 0
end

# Obtain the last line of the logfile by setting an arbitrarily large
# line number as the starting point
log = diagMgr.BrowseDiagnosticLog(key: logKey, start: 999999999)
lineEnd = log.lineEnd

# Get the last 5 lines of the log first, and then check every 2 seconds
# to see if the log size has increased.
start = lineEnd - 5
while true
  log = diagMgr.BrowseDiagnosticLog(key: logKey, start: start)
  if log.lineStart != 0
    log.lineText.each do |l|
      puts l
    end
  end
  start = log.lineEnd + 1
  sleep 2
end