Class: Pvectl::Commands::Logs::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/logs/command.rb

Overview

Dispatcher for the ‘pvectl logs <resource_type> <id>` command.

Routes requests to appropriate log handlers:

  • vm/ct: task history via Handlers::TaskLogs

  • node: syslog via Handlers::Syslog (or journal with –journal)

  • task: log lines via Handlers::TaskDetail

Examples:

Basic usage

Commands::Logs::Command.execute("vm", "100", options, global_options)

Constant Summary collapse

VM_CT_TYPES =

VM/CT resource types that use TaskLogs handler.

%w[vm vms ct container containers cts].freeze
NODE_TYPES =

Node resource types.

%w[node nodes].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_type, resource_id, options, global_options, handler: nil, journal_handler: nil, registry: Logs::ResourceRegistry) ⇒ Command

Creates a new Logs command instance.

Parameters:

  • resource_type (String, nil)

    type of resource

  • resource_id (String, nil)

    resource identifier

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options

  • handler (Object, nil) (defaults to: nil)

    override handler for testing

  • journal_handler (Object, nil) (defaults to: nil)

    override journal handler for testing

  • registry (Class) (defaults to: Logs::ResourceRegistry)

    registry for handler lookup



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pvectl/commands/logs/command.rb', line 131

def initialize(resource_type, resource_id, options, global_options,
               handler: nil, journal_handler: nil,
               registry: Logs::ResourceRegistry)
  @resource_type = resource_type
  @resource_id = resource_id
  @options = options
  @global_options = global_options
  @handler = handler
  @journal_handler = journal_handler
  @registry = registry
end

Class Method Details

.execute(resource_type, resource_id, options, global_options) ⇒ Integer

Executes the logs command.

Parameters:

  • resource_type (String, nil)

    type (vm, ct, node, task)

  • resource_id (String, nil)

    identifier (VMID, node name, UPID)

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



118
119
120
# File 'lib/pvectl/commands/logs/command.rb', line 118

def self.execute(resource_type, resource_id, options, global_options)
  new(resource_type, resource_id, options, global_options).execute
end

.register(cli) ⇒ void

This method returns an undefined value.

Registers the logs command with the CLI.

Parameters:

  • cli (GLI::App)

    the CLI application object



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
# File 'lib/pvectl/commands/logs/command.rb', line 27

def self.register(cli)
  cli.desc "Show logs for resources (task history, syslog, journal)"
  cli.long_desc <<~HELP
    Show logs and task history for cluster resources. The log source
    depends on the resource type:

      vm / container    Task history (start, stop, backup, migrate, etc.)
      node              System log (syslog by default, --journal for systemd)
      task              Detailed log output for a specific task UPID

    EXAMPLES
      Node syslog (last 50 entries):
        $ pvectl logs node pve1

      Node systemd journal:
        $ pvectl logs node pve1 --journal

      Task history for a VM:
        $ pvectl logs vm 100

      Search VM tasks across all nodes:
        $ pvectl logs vm 100 --all-nodes

      Filter by task type and date:
        $ pvectl logs vm 100 --type vzdump --since 2026-01-01

      Detailed log for a specific task:
        $ pvectl logs task UPID:pve1:000ABC:...

      Filter node syslog by service:
        $ pvectl logs node pve1 --service pvedaemon

      Increase entry limit:
        $ pvectl logs node pve1 --limit 200

    NOTES
      Default limit is 50 entries. Task detail (UPID) defaults to 512 lines.

      --all-nodes searches across every cluster node for VM/CT tasks.
      This is useful when a VM has been migrated between nodes.

      Timestamps for --since and --until accept YYYY-MM-DD format
      or Unix epoch seconds.

    SEE ALSO
      pvectl help get tasks     List task history cluster-wide
      pvectl help describe      Detailed resource information
  HELP
  cli.arg_name "RESOURCE_TYPE ID"
  cli.command :logs do |c|
    c.desc "Maximum number of entries to show"
    c.default_value 50
    c.flag [:limit], type: Integer, arg_name: "N"

    c.desc "Show entries since timestamp (YYYY-MM-DD or epoch)"
    c.flag [:since], arg_name: "TIMESTAMP"

    c.desc "Show entries until timestamp (YYYY-MM-DD or epoch)"
    c.flag [:until], arg_name: "TIMESTAMP"

    c.desc "Filter by task type (e.g., qmstart, qmstop, vzdump)"
    c.flag [:type], arg_name: "TYPE"

    c.desc "Filter by status (running, ok, error)"
    c.flag [:status], arg_name: "STATUS"

    c.desc "Filter by service name (syslog only)"
    c.flag [:service], arg_name: "SERVICE"

    c.desc "Use systemd journal instead of syslog (node only)"
    c.switch [:journal], negatable: false

    c.desc "Search across all cluster nodes (VM/CT only)"
    c.switch [:"all-nodes"], negatable: false

    c.action do |global_options, options, args|
      resource_type = args[0]
      resource_id = args[1]
      exit_code = execute(resource_type, resource_id, options, global_options)
      exit exit_code if exit_code != 0
    end
  end
end

Instance Method Details

#executeInteger

Executes the logs operation.

Returns:

  • (Integer)

    exit code



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
# File 'lib/pvectl/commands/logs/command.rb', line 146

def execute
  return missing_resource_type_error if @resource_type.nil?
  return missing_resource_id_error if @resource_id.nil? || @resource_id.empty?

  handler = resolve_handler
  return unknown_resource_error unless handler

  models = fetch_data(handler)
  output = format_output(models, handler.presenter)
  puts output

  ExitCodes::SUCCESS
rescue ResourceNotFoundError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::NOT_FOUND
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONFIG_ERROR
rescue Timeout::Error, Errno::ECONNREFUSED, SocketError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONNECTION_ERROR
end