Skip to content

Commit

Permalink
Record metrics for device manager startup operations (#1218)
Browse files Browse the repository at this point in the history
* Record metrics for device manager startup operations

* Update help text

* Update help text
  • Loading branch information
mlw authored Nov 3, 2023
1 parent d2cbddd commit f9a937a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions Source/santad/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ objc_library(
"//Source/common:SNTCommonEnums",
"//Source/common:SNTDeviceEvent",
"//Source/common:SNTLogging",
"//Source/common:SNTMetricSet",
],
)

Expand Down
33 changes: 33 additions & 0 deletions Source/santad/EventProviders/SNTEndpointSecurityDeviceManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#import "Source/common/SNTDeviceEvent.h"
#import "Source/common/SNTLogging.h"
#import "Source/common/SNTMetricSet.h"
#include "Source/santad/EventProviders/EndpointSecurity/Message.h"
#include "Source/santad/Metrics.h"

Expand All @@ -40,11 +41,18 @@
using santa::santad::event_providers::endpoint_security::Message;
using santa::santad::logs::endpoint_security::Logger;

static NSString *const kMetricStartupDiskOperationSkip = @"Skipped";
static NSString *const kMetricStartupDiskOperationAllowed = @"Allowed";
static NSString *const kMetricStartupDiskOperationUnmountFailed = @"UnmountFailed";
static NSString *const kMetricStartupDiskOperationRemountFailed = @"RemountFailed";
static NSString *const kMetricStartupDiskOperationSuccess = @"Success";

@interface SNTEndpointSecurityDeviceManager ()

- (void)logDiskAppeared:(NSDictionary *)props;
- (void)logDiskDisappeared:(NSDictionary *)props;

@property SNTMetricCounter *startupDiskMetrics;
@property DASessionRef diskArbSession;
@property(nonatomic, readonly) dispatch_queue_t diskQueue;
@property dispatch_semaphore_t diskSema;
Expand Down Expand Up @@ -182,6 +190,20 @@ - (instancetype)initWithESAPI:(std::shared_ptr<EndpointSecurityAPI>)esApi
_diskArbSession = DASessionCreate(NULL);
DASessionSetDispatchQueue(_diskArbSession, _diskQueue);

SNTMetricInt64Gauge *startupPrefsMetric = [[SNTMetricSet sharedInstance]
int64GaugeWithName:@"/santa/device_manager/startup_preference"
fieldNames:@[]
helpText:@"The current startup preference value"];

[[SNTMetricSet sharedInstance] registerCallback:^{
[startupPrefsMetric set:startupPrefs forFieldValues:@[]];
}];

_startupDiskMetrics = [[SNTMetricSet sharedInstance]
counterWithName:@"/santa/device_manager/startup_disk_operation"
fieldNames:@[ @"operation" ]
helpText:@"Count of the number of USB devices encountered per operation"];

[self performStartupTasks:startupPrefs];

[self establishClientOrDie];
Expand Down Expand Up @@ -233,6 +255,10 @@ - (BOOL)remountUSBModeContainsFlags:(uint32_t)flags {
return (flags & requiredFlags) == requiredFlags;
}

- (void)incrementStartupMetricsOperation:(NSString *)op {
[self.startupDiskMetrics incrementForFieldValues:@[ op ]];
}

// NB: Remount options are implemented as separate "unmount" and "mount"
// operations instead of using the "update"/MNT_UPDATE flag. This is because
// filesystems often don't support many transitions (e.g. RW to RO). Performing
Expand Down Expand Up @@ -268,12 +294,14 @@ - (void)performStartupTasks:(SNTDeviceManagerStartupPreferences)startupPrefs {
CFAutorelease(disk);

if (![self shouldOperateOnDisk:disk]) {
[self incrementStartupMetricsOperation:kMetricStartupDiskOperationSkip];
continue;
}

if (self.remountArgs != nil && [self remountUSBModeContainsFlags:sfs->f_flags]) {
LOGI(@"Allowing existing mount as flags contain RemountUSBMode. '%s' -> '%s'",
sfs->f_mntfromname, sfs->f_mntonname);
[self incrementStartupMetricsOperation:kMetricStartupDiskOperationAllowed];
continue;
}

Expand All @@ -293,6 +321,7 @@ - (void)performStartupTasks:(SNTDeviceManagerStartupPreferences)startupPrefs {
LOGW(
@"Unmounting '%s' mounted on '%s' took longer than expected. Device may still be mounted.",
sfs->f_mntfromname, sfs->f_mntonname);
[self incrementStartupMetricsOperation:kMetricStartupDiskOperationUnmountFailed];
continue;
}

Expand All @@ -307,8 +336,12 @@ - (void)performStartupTasks:(SNTDeviceManagerStartupPreferences)startupPrefs {
if (dispatch_semaphore_wait(self.diskSema,
dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC))) {
LOGW(@"Failed to remount device after unmounting: %s", sfs->f_mntfromname);
[self incrementStartupMetricsOperation:kMetricStartupDiskOperationRemountFailed];
continue;
}
}

[self incrementStartupMetricsOperation:kMetricStartupDiskOperationSuccess];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

using santa::santad::event_providers::endpoint_security::EndpointSecurityAPI;
using santa::santad::event_providers::endpoint_security::EnrichedClose;
using santa::santad::event_providers::endpoint_security::EnrichedCSInvalidated;
using santa::santad::event_providers::endpoint_security::EnrichedExchange;
using santa::santad::event_providers::endpoint_security::EnrichedExec;
using santa::santad::event_providers::endpoint_security::EnrichedExit;
Expand All @@ -45,7 +46,6 @@
using santa::santad::event_providers::endpoint_security::EnrichedProcess;
using santa::santad::event_providers::endpoint_security::EnrichedRename;
using santa::santad::event_providers::endpoint_security::EnrichedUnlink;
using santa::santad::event_providers::endpoint_security::EnrichedCSInvalidated;
using santa::santad::event_providers::endpoint_security::Message;
using santa::santad::logs::endpoint_security::serializers::Utilities::MountFromName;
using santa::santad::logs::endpoint_security::serializers::Utilities::NonNull;
Expand Down
2 changes: 1 addition & 1 deletion Source/santad/Metrics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
fieldNames:@[ @"Processor" ]
helpText:@"Events rate limited by each processor"];

SNTMetricCounter *faa_event_counts = [[SNTMetricSet sharedInstance]
SNTMetricCounter *faa_event_counts = [metric_set
counterWithName:@"/santa/file_access_authorizer/log/count"
fieldNames:@[
@"config_version", @"access_type", @"rule_id", @"status", @"operation", @"decision"
Expand Down

0 comments on commit f9a937a

Please sign in to comment.