Có phương thức/API nào được xác định để thu thập thông tin hệ thống trong OSX hay không. Tôi muốn viết tiện ích sẽ thu thập thông tin phần cứng như CPU, RAM, Bộ điều hợp mạng. Bất kỳ ý tưởng nào? Cảm ơn trước.cách thu thập thông tin hệ thống trong osx bằng cách sử dụng mục tiêu c
Trả lời
Cách đơn giản nhất là sử dụng đầu ra từ lệnh system_profiler. Nó cũng có một tùy chọn -xml
để làm cho đầu ra dễ dàng tự động phân tích cú pháp.
Các liên kết sau đây nên là đủ để giúp bạn bắt đầu:
liên kết này cung cấp lỗi 404. vui lòng cập nhật liên kết –
Bạn có thể sử dụng scripting bridge trong Leopard (hoặc mới hơn) để nhận thông tin bạn muốn trực tiếp từ Apple System Profiler.
API cơ bản mà tôi tin rằng System Profiler sử dụng (cho ít nhất một số thông tin thu thập được) và bạn nên sử dụng nếu bạn muốn có thông tin rất cụ thể, là sysctl. Nó cho phép bạn truy vấn các thuộc tính riêng lẻ của hệ thống, bao gồm số CPU, tốc độ CPU, RAM sẵn có, v.v.
Profiler hệ thống rất đẹp và sẽ xuất ra XML cho một số tệp I/O chậm và cũng phụ thuộc vào quá trình hoàn thành trước khi nhận được thông tin mong muốn. Vâng, nếu tôi đặt nó theo cách này, là System Profiler thực sự là lựa chọn tốt nhất và câu trả lời cho câu hỏi này? Tôi nghĩ không (IMO).
Đây là cách tôi thực hiện. Tiêu đề là thuộc tính chỉ đọc của thuộc tính ghi đè riêng tư. Các phương pháp thể loại khá thẳng về phía trước, nhưng nếu có ai có câu hỏi thì hãy đăng bài và tôi sẽ trả lời.
#import <IOKit/IOKitLib.h>
#import <sys/sysctl.h>
@interface VarSystemInfo()
@property (readwrite, strong, nonatomic) NSString *sysName;
@property (readwrite, strong, nonatomic) NSString *sysUserName;
@property (readwrite, strong, nonatomic) NSString *sysFullUserName;
@property (readwrite, strong, nonatomic) NSString *sysOSName;
@property (readwrite, strong, nonatomic) NSString *sysOSVersion;
@property (readwrite, strong, nonatomic) NSString *sysPhysicalMemory;
@property (readwrite, strong, nonatomic) NSString *sysSerialNumber;
@property (readwrite, strong, nonatomic) NSString *sysUUID;
@property (readwrite, strong, nonatomic) NSString *sysModelID;
@property (readwrite, strong, nonatomic) NSString *sysModelName;
@property (readwrite, strong, nonatomic) NSString *sysProcessorName;
@property (readwrite, strong, nonatomic) NSString *sysProcessorSpeed;
@property (readwrite, strong, nonatomic) NSNumber *sysProcessorCount;
@property (readonly, strong, nonatomic) NSString *getOSVersionInfo;
- (NSString *) _strIORegistryEntry:(NSString *)registryKey;
- (NSString *) _strControlEntry:(NSString *)ctlKey;
- (NSNumber *) _numControlEntry:(NSString *)ctlKey;
- (NSString *) _modelNameFromID:(NSString *)modelID;
- (NSString *) _parseBrandName:(NSString *)brandName;
@end
static NSString* const kVarSysInfoVersionFormat = @"%@.%@.%@ (%@)";
static NSString* const kVarSysInfoPlatformExpert = @"IOPlatformExpertDevice";
static NSString* const kVarSysInfoKeyOSVersion = @"kern.osrelease";
static NSString* const kVarSysInfoKeyOSBuild = @"kern.osversion";
static NSString* const kVarSysInfoKeyModel = @"hw.model";
static NSString* const kVarSysInfoKeyCPUCount = @"hw.physicalcpu";
static NSString* const kVarSysInfoKeyCPUFreq = @"hw.cpufrequency";
static NSString* const kVarSysInfoKeyCPUBrand = @"machdep.cpu.brand_string";
static NSString* const kVarSysInfoMachineNames = @"MachineNames";
static NSString* const kVarSysInfoMachineiMac = @"iMac";
static NSString* const kVarSysInfoMachineMacmini = @"Mac mini";
static NSString* const kVarSysInfoMachineMacBookAir = @"MacBook Air";
static NSString* const kVarSysInfoMachineMacBookPro = @"MacBook Pro";
static NSString* const kVarSysInfoMachineMacPro = @"Mac Pro";
#pragma mark - Implementation:
#pragma mark -
@implementation VarSystemInfo
@synthesize sysName, sysUserName, sysFullUserName;
@synthesize sysOSName, sysOSVersion;
@synthesize sysPhysicalMemory;
@synthesize sysSerialNumber, sysUUID;
@synthesize sysModelID, sysModelName;
@synthesize sysProcessorName, sysProcessorSpeed, sysProcessorCount;
#pragma mark - Helper Methods:
- (NSString *) _strIORegistryEntry:(NSString *)registryKey {
NSString *retString;
io_service_t service =
IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching([kVarSysInfoPlatformExpert UTF8String]));
if (service) {
CFTypeRef cfRefString =
IORegistryEntryCreateCFProperty(service,
(__bridge CFStringRef)registryKey,
kCFAllocatorDefault, kNilOptions);
if (cfRefString) {
retString = [NSString stringWithString:(__bridge NSString *)cfRefString];
CFRelease(cfRefString);
} IOObjectRelease(service);
} return retString;
}
- (NSString *) _strControlEntry:(NSString *)ctlKey {
size_t size = 0;
if (sysctlbyname([ctlKey UTF8String], NULL, &size, NULL, 0) == -1) return nil;
char *machine = calloc(1, size);
sysctlbyname([ctlKey UTF8String], machine, &size, NULL, 0);
NSString *ctlValue = [NSString stringWithCString:machine encoding:[NSString defaultCStringEncoding]];
free(machine); return ctlValue;
}
- (NSNumber *) _numControlEntry:(NSString *)ctlKey {
size_t size = sizeof(uint64_t); uint64_t ctlValue = 0;
if (sysctlbyname([ctlKey UTF8String], &ctlValue, &size, NULL, 0) == -1) return nil;
return [NSNumber numberWithUnsignedLongLong:ctlValue];
}
- (NSString *) _modelNameFromID:(NSString *)modelID {
/*!
* @discussion Maintain Machine Names plist from the following site
* @abstract ref: http://www.everymac.com/systems/by_capability/mac-specs-by-machine-model-machine-id.html
*
* @discussion Also info found in SPMachineTypes.plist @ /System/Library/PrivateFrameworks/...
* ...AppleSystemInfo.framework/Versions/A/Resources
* Information here is private and can not be linked into the code.
*/
NSDictionary *modelDict = [[NSBundle mainBundle] URLForResource:kVarSysInfoMachineNames withExtension:@"plist"].serialPList;
NSString *modelName = [modelDict objectForKey:modelID];
if (!modelName) {
if ([modelID.lowercaseString hasPrefix:kVarSysInfoMachineiMac.lowercaseString]) return kVarSysInfoMachineiMac;
else if ([modelID.lowercaseString hasPrefix:kVarSysInfoMachineMacmini.noWhitespaceAndLowerCaseString]) return kVarSysInfoMachineMacmini;
else if ([modelID.lowercaseString hasPrefix:kVarSysInfoMachineMacBookAir.noWhitespaceAndLowerCaseString]) return kVarSysInfoMachineMacBookAir;
else if ([modelID.lowercaseString hasPrefix:kVarSysInfoMachineMacBookPro.noWhitespaceAndLowerCaseString]) return kVarSysInfoMachineMacBookPro;
else if ([modelID.lowercaseString hasPrefix:kVarSysInfoMachineMacPro.noWhitespaceAndLowerCaseString]) return kVarSysInfoMachineMacPro;
else return modelID;
} return modelName;
}
- (NSString *) _parseBrandName:(NSString *)brandName {
if (!brandName) return nil;
NSMutableArray *newWords = [NSMutableArray array];
NSString *strCopyRight = @"r", *strTradeMark = @"tm", *strCPU = @"CPU";
NSArray *words = [brandName componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]];
for (NSString *word in words) {
if ([word isEqualToString:strCPU]) break;
if ([word isEqualToString:@""]) continue;
if ([word.lowercaseString isEqualToString:strCopyRight]) continue;
if ([word.lowercaseString isEqualToString:strTradeMark]) continue;
if ([word length] > 0) {
NSString *firstChar = [word substringToIndex:1];
if (NSNotFound != [firstChar rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location) continue;
[newWords addObject:word];
} } return [newWords componentsJoinedByString:@" "];
}
- (NSString *) getOSVersionInfo {
NSString *darwinVer = [self _strControlEntry:kVarSysInfoKeyOSVersion];
NSString *buildNo = [self _strControlEntry:kVarSysInfoKeyOSBuild];
if (!darwinVer || !buildNo) return nil;
NSString *majorVer = @"10", *minorVer = @"x", *bugFix = @"x";
NSArray *darwinChunks = [darwinVer componentsSeparatedByCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
if ([darwinChunks count] > 0) {
NSInteger firstChunk = [(NSString *)[darwinChunks objectAtIndex:0] integerValue];
minorVer = [NSString stringWithFormat:@"%ld", (firstChunk - 4)];
bugFix = [darwinChunks objectAtIndex:1];
return [NSString stringWithFormat:kVarSysInfoVersionFormat, majorVer, minorVer, bugFix, buildNo];
} return nil;
}
#pragma mark - Initalization:
- (void) setupSystemInformation {
NSProcessInfo *pi = [NSProcessInfo processInfo];
self.sysName = [[NSHost currentHost] localizedName];
self.sysUserName = NSUserName();
self.sysFullUserName = NSFullUserName();
self.sysOSName = pi.strOperatingSystem;
self.sysOSVersion = self.getOSVersionInfo;
self.sysPhysicalMemory = [[NSNumber numberWithUnsignedLongLong:pi.physicalMemory] strBinarySizeMaxFractionDigits:0];
self.sysSerialNumber = [self _strIORegistryEntry:(__bridge NSString *)CFSTR(kIOPlatformSerialNumberKey)];
self.sysUUID = [self _strIORegistryEntry:(__bridge NSString *)CFSTR(kIOPlatformUUIDKey)];
self.sysModelID = [self _strControlEntry:kVarSysInfoKeyModel];
self.sysModelName = [self _modelNameFromID:self.sysModelID];
self.sysProcessorName = [self _parseBrandName:[self _strControlEntry:kVarSysInfoKeyCPUBrand]];
self.sysProcessorSpeed = [[self _numControlEntry:kVarSysInfoKeyCPUFreq] strBaseTenSpeedMaxFractionDigits:2];
self.sysProcessorCount = [self _numControlEntry:kVarSysInfoKeyCPUCount];
}
- (id) init {
if ((self = [super init])) {
[self setupSystemInformation];
} return self;
}
@end
Tận hưởng!
P.S. Tôi tải tất cả các giá trị thuộc tính trong init để tránh nhiều cuộc gọi hệ thống & & vì giá trị & & giá rẻ của tất cả các giá trị phải là khá tĩnh.
P.P.S. Tôi cũng tải một plist MachineNames mà tôi tạo ra, nhưng tôi biết quá trình của riêng tôi chỉ có quyền truy cập vào nó và nhận xét mô tả nơi tôi nhận được thông tin.
trước hết, @Arvin bạn thật tuyệt vời và tiết kiệm cuộc sống. Tôi cần sự chú ý của bạn trên một số mã của bạn vì một số dòng có lỗi và các dòng đó là: Lỗi 1: self.sysOSName = pi.strOperatingSystem; Lỗi 2: self.sysPhysicalMemory = [[Số NSNumberWithUnsignedLongLong: pi.physicalMemory] strBinarySizeMaxFractionDigits: 0]; Lỗi 3: self.sysModelName = [self _modelNameFromID: self.sysModelID]; Lỗi 4: self.sysProcessorSpeed = [[self _numControlEntry: kVarSysInfoKeyCPUFreq] strBaseTenSpeedMaxFractionDigits: 2]; –
- 1. Thu thập thông tin hệ thống và môi trường
- 2. Cách tạo start_urls động trong thu thập thông tin?
- 3. Thu thập thông tin Internet
- 4. Tôi làm cách nào để sử dụng Scrapy để thu thập thông tin trong các trang?
- 5. Tăng thu thập thông tin/lập chỉ mục của Google?
- 6. Cách thu thập thông tin tương tự như panopticlick.eff.org
- 7. Sử dụng THU THẬP SỐ LIỆU THỐNG KÊ trong Teradata
- 8. Cách tốt nhất để thu thập số liệu thống kê sử dụng ứng dụng Rails
- 9. Phát triển trình thu thập thông tin và trình thu thập thông tin cho công cụ tìm kiếm dọc
- 10. Làm cách nào để cho phép trình thu thập thông tin truy cập vào chỉ mục index.php, sử dụng robots.txt?
- 11. Làm cách nào để đọc thông tin hệ thống trong C++?
- 12. Thu thập thông tin cửa hàng Google Play
- 13. Điều chỉnh một phương pháp hệ thống thông qua các loại trong Mục tiêu-C?
- 14. Thu thập thông tin không chính xác CrawlSpider không thu thập thông tin trang đích đầu tiên
- 15. Làm cách nào để sao chép thư mục bằng cách sử dụng Hệ thống tập tin tăng cường
- 16. Trình thu thập thông tin web trong ruby
- 17. Nhận thông tin múi giờ của hệ thống bằng Python?
- 18. Cách tăng :: hệ thống :: error_code :: tin nhắn bằng tiếng Anh?
- 19. Cách truy cập hệ thống tập tin bằng java?
- 20. Đọc thông tin Dock OSX?
- 21. Cách sử dụng giao thức C mục tiêu
- 22. Cách sử dụng createReadStream của hệ thống tập tin bằng bộ định tuyến Meteor (NodeJS)
- 23. Asp.net Request.Browser.Crawler - Danh sách trình thu thập thông tin động?
- 24. Làm cách nào để lấy thông tin hệ thống bằng Python?
- 25. Sử dụng bộ nhớ của OSX bằng cách sử dụng Ruby
- 26. Cách thu thập số liệu thống kê sử dụng ký tự trong tệp văn bản bằng các lệnh Unix?
- 27. Cách thu thập dữ liệu Facebook dựa trên thông tin về tình bạn?
- 28. Làm GWT ứng dụng thu thập thông tin bằng công cụ tìm kiếm
- 29. Grails: cách thu thập tất cả các tin nhắn
- 30. Trình thu thập thông tin web - Bỏ qua tệp Robots.txt?
Điều này cung cấp lượng thông tin tốt. Cám ơn vì cái này. Chỉ cần tự hỏi rằng nếu có bất kỳ API nào để truy vấn thông tin cụ thể. Giống như chỉ cho bộ nhớ hoặc cho phép nói bộ vi xử lý. – Unicorn
Đề nghị bạn đọc trang người đàn ông: 'system_profiler -listDataTypes', sau đó' system_profiler -xml dataType1 ... ' –
Cảm ơn ned, tôi đã tìm thấy những gì tôi đang tìm kiếm. Tôi nghĩ rằng có thể bắt đầu sử dụng điều này ngay bây giờ. – Unicorn