Module: Jamf::ManagementHistory::ClassMethods

Defined in:
lib/jamf/api/classic/api_objects/management_history.rb

Overview

See codereview.stackexchange.com/questions/23637/mixin-both-instance-and-class-methods-in-ruby for discussion of this technique for mixing in both Class and Instance methods when including a module.

Instance Method Summary collapse

Instance Method Details

#app_store_app_history(ident, status = nil, api: nil, cnx: Jamf.cnx) ⇒ Array Also known as: managed_app_history

Wrapper for app store history for both computers and mobile devices

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • status (Symbol) (defaults to: nil)

    Return only the :installed, :pending, or :failed apps

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

  • (Array)

    An array of MacAppStoreApp or MobileDeviceApp



471
472
473
474
475
476
477
478
479
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 471

def app_store_app_history(ident, status = nil, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  if self == Jamf::MobileDevice
    mobile_device_app_history(ident, status, cnx: cnx)
  else
    mac_app_store_app_history(ident, status, cnx: cnx)
  end
end

#audit_history(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::AuditEvent> Also known as: audits

The history of Audit events for a target

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:



225
226
227
228
229
230
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 225

def audit_history(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  hist = management_history(ident, :audits, cnx: cnx)
  hist.map! { |aud| Jamf::ManagementHistory::AuditEvent.new aud }
end

#casper_imaging_logs(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::CasperImagingLog>

The history of Casper Imaging events for a computer

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



524
525
526
527
528
529
530
531
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 524

def casper_imaging_logs(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only computers have casper imaging logs' unless self == Jamf::Computer

  hist = management_history(ident, :casper_imaging_logs, cnx: cnx)
  hist.map! { |evt| Jamf::ManagementHistory::CasperImagingLog.new evt }
end

#casper_remote_logs(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::CasperRemoteLog>

The history of Casper Remote events for a computer

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



542
543
544
545
546
547
548
549
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 542

def casper_remote_logs(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only computers have casper remote logs' unless self == Jamf::Computer

  hist = management_history(ident, :casper_remote_logs, cnx: cnx)
  hist.map! { |evt| Jamf::ManagementHistory::CasperRemoteLog.new evt }
end

#completed_mdm_commands(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::MdmCommand> Also known as: completed_commands

The history of completed mdm commands for a target

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:



301
302
303
304
305
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 301

def completed_mdm_commands(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  mdm_command_history(ident, :completed, cnx: cnx)
end

#completed_policies(ident, api: nil, cnx: Jamf.cnx) ⇒ Object

The array from .policy_logs, limited to status = :completed

See Also:

  • ManagementHistory::ClassMethods.policy_logs


609
610
611
612
613
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 609

def completed_policies(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  policy_logs(ident, cnx: cnx).select { |pl| pl.status == :completed }
end

#computer_usage_logs(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::ComputerUsageLog> Also known as: usage_logs

The history of usage events for a computer

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



560
561
562
563
564
565
566
567
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 560

def computer_usage_logs(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only computers have usage logs' unless self == Jamf::Computer

  hist = management_history(ident, :computer_usage_logs, cnx: cnx)
  hist.map! { |evt| Jamf::ManagementHistory::ComputerUsageLog.new evt }
end

#ebook_history(ident, status = nil, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::Ebook> Also known as: managed_ebook_history

The history of ebooks for a mobile device

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • status (Symbol) (defaults to: nil)

    Return only the :installed, :pending, or :failed apps

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



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
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 635

def ebook_history(ident, status = nil, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only mobile devices have ebooks' unless self == Jamf::MobileDevice

  hist = management_history(ident, :ebooks, cnx: cnx)
  if status
    raise Jamf::InvalidDataError, 'status must be one of :installed, :pending, or :failed' unless HIST_APP_STATUSES.include? status

    statuses_to_do = [status]
  else
    statuses_to_do = HIST_APP_STATUSES
  end # if status

  result = []

  statuses_to_do.each do |a_status|
    # merge the sources of installed apps into their hashes
    books =
      if a_status == :installed
        instbooks = []
        hist[a_status].each do |src, books_from_src|
          real_src =
            case src
            when HIST_RAW_SOURCE_EBOOK_IN_HOUSE then HIST_SOURCE_IN_HOUSE
            when HIST_RAW_SOURCE_IBOOKSTORE then HIST_SOURCE_IBOOKSTORE
            else HIST_SOURCE_OTHER
            end # case src
          instbooks += books_from_src.map! { |book| book[:source] = real_src }
        end
        instbooks
      else
        hist[a_status]
      end
    # now 'books' is an array of hashes of books with the same status
    # and if they are :installed, their source is in the hash

    # merge the statuses of the books into their hashes
    result += books.map! do |book|
      # set the :status in the hash
      book[:status] = a_status
      Jamf::ManagementHistory::Ebook.new book
    end # map do |books|
  end # statuses_to_do.each do |a_status|

  result
end

#failed_app_store_apps(ident, api: nil, cnx: Jamf.cnx) ⇒ Object Also known as: failed_managed_apps

shortcut for app_store_app_history where status = :failed



508
509
510
511
512
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 508

def failed_app_store_apps(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  app_store_app_history(ident, :failed, cnx: cnx)
end

#failed_ebooks(ident, api: nil, cnx: Jamf.cnx) ⇒ Object Also known as: failed_managed_ebooks

shortcut for ebook_history where status = :failed

See Also:



710
711
712
713
714
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 710

def failed_ebooks(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  ebook_history(ident, :failed, cnx: cnx)
end

#failed_mdm_commands(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::MdmCommand> Also known as: failed_commands

The history of failed mdm commands for a target

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:



333
334
335
336
337
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 333

def failed_mdm_commands(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  mdm_command_history(ident, :failed, cnx: cnx)
end

#failed_policies(ident, api: nil, cnx: Jamf.cnx) ⇒ Object

The array from .policy_logs, limited to status = :failed

See Also:

  • ManagementHistory::ClassMethods.policy_logs


618
619
620
621
622
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 618

def failed_policies(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  policy_logs(ident, cnx: cnx).select { |pl| pl.status == :failed }
end

#installed_app_store_apps(ident, api: nil, cnx: Jamf.cnx) ⇒ Object Also known as: installed_managed_apps

shortcut for app_store_app_history where status = :installed



486
487
488
489
490
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 486

def installed_app_store_apps(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  app_store_app_history(ident, :installed, cnx: cnx)
end

#installed_ebooks(ident, api: nil, cnx: Jamf.cnx) ⇒ Object Also known as: installed_managed_ebooks

shortcut for ebook_history where status = :installed

See Also:



688
689
690
691
692
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 688

def installed_ebooks(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  ebook_history(ident, :installed, cnx: cnx)
end

#last_mdm_contact(ident, api: nil, cnx: Jamf.cnx) ⇒ Time?

The time of the most recently completed or failed MDM command. (knowledge of a failure means the device communicated with us)

For Mobile Devices, this seems to be the best indicator of the real last-contact time, since the last_inventory_update is changed when changes are made via the API.

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

  • (Time, nil)

    An array of completed MdmCommands



354
355
356
357
358
359
360
361
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 354

def last_mdm_contact(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  epochs = completed_mdm_commands(ident, cnx: cnx).map { |cmd| cmd.completed_epoch }
  epochs += failed_mdm_commands(ident, cnx: cnx).map { |cmd| cmd.failed_epoch }
  epoch = epochs.max
  epoch ? JSS.epoch_to_time(epoch) : nil
end

#mac_app_store_app_history(ident, status = nil, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::MacAppStoreApp>

The history of app store apps for a computer

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • status (Symbol) (defaults to: nil)

    Return only the :installed, :pending, or :failed apps

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



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
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 374

def mac_app_store_app_history(ident, status = nil, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only computers have mac app store apps' unless self == Jamf::Computer

  hist = management_history(ident, :mac_app_store_applications, cnx: cnx)
  if status
    raise Jamf::InvalidDataError, 'status must be one of :installed, :pending, or :failed' unless HIST_APP_STATUSES.include? status

    statuses_to_do = [status]
  else
    statuses_to_do = HIST_APP_STATUSES
  end # if status

  result = []

  statuses_to_do.each do |a_status|
    result += hist[a_status].map! do |app|
      # set the :status
      app[:status] = a_status
      Jamf::ManagementHistory::MacAppStoreApp.new app
    end # map do |cmd|
  end # statuses_to_do.each do |a_status|

  result
end

#management_history(ident, subset = nil, api: nil, cnx: Jamf.cnx) ⇒ Hash, Array Also known as: history

Return the raw management history for a Computer or Mobile Device

WARNING: It’s huge, better to use a subset.

NOTE: This returns the raw JSON data from the API, parsed into a ruby Hash. Use the subset-specific methods to retrieve more consistent arrays of ruby-jss objects for each kind of history event, e.g. Jamf::Computer.audits(id) or Jamf::MobileDevice.audits(id) to get an array of Jamf::ManagementHistory::AuditEvent objects

Parameters:

  • ident (Integer, String)

    An identifier (id, name, serialnumber, macadress or udid) of the computer or device for which to retrieve history

  • subset (Symbol) (defaults to: nil)

    the subset to return, rather than full history.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    an API connection to use for the query. Defaults to the corrently active API. See Connection

Returns:

  • (Hash, Array)

    The raw full history or subset requested

Raises:



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
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 189

def management_history(ident, subset = nil, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  id = valid_id ident, cnx: cnx
  raise Jamf::NoSuchItemError, "No #{self::RSRC_OBJECT_KEY} matches identifier: #{ident}" unless id

  if self == Jamf::Computer
    @hist_subsets ||= HIST_COMPUTER_SUBSETS
    @hist_rsrc ||= HIST_COMPUTER_RSRC
    @hist_key ||= HIST_COMPUTER_KEY
  else
    @hist_subsets ||= HIST_DEVICE_SUBSETS
    @hist_rsrc ||= HIST_DEVICE_RSRC
    @hist_key ||= HIST_DEVICE_KEY
  end

  if subset
    raise "Subset must be one of :#{@hist_subsets.join ', :'}" unless @hist_subsets.include? subset

    subset_rsrc = @hist_rsrc + "/id/#{id}/subset/#{subset}"
    cnx.c_get(subset_rsrc)[@hist_key][subset]
  else
    cnx.c_get(@hist_rsrc + "/id/#{id}")[@hist_key]
  end
end

#mdm_command_history(ident, status = nil, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::MdmCommand> Also known as: commands, management_command_history

The history of mdm commands for a target

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • status (Symbol) (defaults to: nil)

    Return only the :completed, :pending, or :failed commands

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:



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
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 260

def mdm_command_history(ident, status = nil, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  subset = self == Jamf::Computer ? :commands : :management_commands
  hist = management_history(ident, subset, cnx: cnx)
  if status
    raise Jamf::InvalidDataError, 'status must be one of :completed, :pending, or :failed' unless HIST_MDM_STATUSES.include? status

    statuses_to_do = [status]
  else
    statuses_to_do = HIST_MDM_STATUSES
  end # if status

  result = []

  statuses_to_do.each do |a_status|
    result += hist[a_status].map! do |cmd|
      # failed computer cmds have the error message in cmd[:status]
      # failed mdm commands have them in cmd[:error], where they should be
      cmd[:error] ||= cmd[:status] if a_status == :failed

      # but we always set the :status
      cmd[:status] = a_status

      Jamf::ManagementHistory::MdmCommand.new(**cmd)
    end # map do |cmd|
  end # statuses_to_do.each do |a_status|
  result
end

#mobile_device_app_history(ident, status = nil, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::MobileDeviceApp>

The history of apps for a mobile device

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • status (Symbol) (defaults to: nil)

    Return only the :installed, :pending, or :failed apps

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



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
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 412

def mobile_device_app_history(ident, status = nil, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only mobile devices have mobile device apps' unless self == Jamf::MobileDevice

  hist = management_history(ident, :applications, cnx: cnx)
  if status
    raise Jamf::InvalidDataError, 'status must be one of :installed, :pending, or :failed' unless HIST_APP_STATUSES.include? status

    statuses_to_do = [status]
  else
    statuses_to_do = HIST_APP_STATUSES
  end # if status

  result = []

  statuses_to_do.each do |a_status|
    # merge the sources of installed apps into their hashes
    apps =
      if a_status == :installed
        instapps = []
        hist[a_status].each do |src, apps_from_src|
          real_src =
            case src
            when HIST_RAW_SOURCE_APP_IN_HOUSE then HIST_SOURCE_IN_HOUSE
            when HIST_RAW_SOURCE_APP_STORE then HIST_SOURCE_APP_STORE
            else HIST_SOURCE_OTHER
            end # case src
          instapps += apps_from_src.map! { |iapp| iapp[:source] = real_src }
        end
        instapps
      else
        hist[a_status]
      end
    # now 'apps' is an array of hashes of apps with the same status
    # and if they are :installed, their source is in the hash

    # merge the statuses of the apps into their hashes
    result += apps.map! do |app|
      # set the :status in the hash
      app[:status] = a_status
      Jamf::ManagementHistory::MobileDeviceApp.new app
    end # map do |cmd|
  end # statuses_to_do.each do |a_status|

  result
end

#pending_app_store_apps(ident, api: nil, cnx: Jamf.cnx) ⇒ Object Also known as: pending_managed_apps

shortcut for app_store_app_history where status = :pending



497
498
499
500
501
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 497

def pending_app_store_apps(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  app_store_app_history(ident, :pending, cnx: cnx)
end

#pending_ebooks(ident, api: nil, cnx: Jamf.cnx) ⇒ Object Also known as: pending_managed_ebooks

shortcut for ebook_history where status = :pending

See Also:



699
700
701
702
703
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 699

def pending_ebooks(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  ebook_history(ident, :pending, cnx: cnx)
end

#pending_mdm_commands(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::MdmCommand> Also known as: pending_commands

The history of pending mdm commands for a target

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:



317
318
319
320
321
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 317

def pending_mdm_commands(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  mdm_command_history(ident, :pending, cnx: cnx)
end

#policy_logs(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::PolicyLog>

The history of policy execution for a computer

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



597
598
599
600
601
602
603
604
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 597

def policy_logs(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only computers have policy logs' unless self == Jamf::Computer

  hist = management_history(ident, :policy_logs, cnx: cnx)
  hist.map! { |evt| Jamf::ManagementHistory::PolicyLog.new(**evt) }
end

#screen_sharing_logs(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::ScreenSharingLog>

The history of screen sharing events for a computer

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:

Raises:



579
580
581
582
583
584
585
586
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 579

def screen_sharing_logs(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  raise Jamf::UnsupportedError, 'Only computers have screen sharing logs' unless self == Jamf::Computer

  hist = management_history(ident, :screen_sharing_logs, cnx: cnx)
  hist.map! { |evt| Jamf::ManagementHistory::ScreenSharingLog.new evt }
end

#user_location_history(ident, api: nil, cnx: Jamf.cnx) ⇒ Array<Jamf::ManagementHistory::UserLocationChange>

The history of User/Location changes for a target

Parameters:

  • ident (Type)

    The identifier for the object - id, name, sn, udid, etc.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    The API connection to use for the query defaults to the currently active connection

Returns:



242
243
244
245
246
247
# File 'lib/jamf/api/classic/api_objects/management_history.rb', line 242

def user_location_history(ident, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  hist = management_history(ident, :user_location, cnx: cnx)
  hist.map! { |evt| Jamf::ManagementHistory::UserLocationChange.new evt }
end