]>
Commit | Line | Data |
---|---|---|
1 | import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core'; | |
2 | ||
3 | import { NgbNav } from '@ng-bootstrap/ng-bootstrap'; | |
4 | import _ from 'lodash'; | |
5 | ||
6 | import { HostService } from '~/app/shared/api/host.service'; | |
7 | import { OsdService } from '~/app/shared/api/osd.service'; | |
8 | import { CdTableColumn } from '~/app/shared/models/cd-table-column'; | |
9 | import { | |
10 | AtaSmartDataV1, | |
11 | IscsiSmartDataV1, | |
12 | NvmeSmartDataV1, | |
13 | SmartDataResult, | |
14 | SmartError, | |
15 | SmartErrorResult | |
16 | } from '~/app/shared/models/smart'; | |
17 | ||
18 | @Component({ | |
19 | selector: 'cd-smart-list', | |
20 | templateUrl: './smart-list.component.html', | |
21 | styleUrls: ['./smart-list.component.scss'] | |
22 | }) | |
23 | export class SmartListComponent implements OnInit, OnChanges { | |
24 | @ViewChild('innerNav') | |
25 | nav: NgbNav; | |
26 | ||
27 | @Input() | |
28 | osdId: number = null; | |
29 | @Input() | |
30 | hostname: string = null; | |
31 | ||
32 | loading = false; | |
33 | incompatible = false; | |
34 | error = false; | |
35 | ||
36 | data: { [deviceId: string]: SmartDataResult | SmartErrorResult } = {}; | |
37 | ||
38 | smartDataColumns: CdTableColumn[]; | |
39 | scsiSmartDataColumns: CdTableColumn[]; | |
40 | ||
41 | isEmpty = _.isEmpty; | |
42 | ||
43 | constructor(private osdService: OsdService, private hostService: HostService) {} | |
44 | ||
45 | isSmartError(data: any): data is SmartError { | |
46 | return _.get(data, 'error') !== undefined; | |
47 | } | |
48 | ||
49 | isNvmeSmartData(data: any): data is NvmeSmartDataV1 { | |
50 | return _.get(data, 'device.protocol', '').toLowerCase() === 'nvme'; | |
51 | } | |
52 | ||
53 | isAtaSmartData(data: any): data is AtaSmartDataV1 { | |
54 | return _.get(data, 'device.protocol', '').toLowerCase() === 'ata'; | |
55 | } | |
56 | ||
57 | isIscsiSmartData(data: any): data is IscsiSmartDataV1 { | |
58 | return _.get(data, 'device.protocol', '').toLowerCase() === 'scsi'; | |
59 | } | |
60 | ||
61 | private fetchData(data: any) { | |
62 | const result: { [deviceId: string]: SmartDataResult | SmartErrorResult } = {}; | |
63 | _.each(data, (smartData, deviceId) => { | |
64 | if (this.isSmartError(smartData)) { | |
65 | let userMessage = ''; | |
66 | if (smartData.smartctl_error_code === -22) { | |
67 | userMessage = $localize`Smartctl has received an unknown argument \ | |
68 | (error code ${smartData.smartctl_error_code}). \ | |
69 | You may be using an incompatible version of smartmontools. Version >= 7.0 of \ | |
70 | smartmontools is required to successfully retrieve data.`; | |
71 | } else { | |
72 | userMessage = $localize`An error with error code ${smartData.smartctl_error_code} occurred.`; | |
73 | } | |
74 | const _result: SmartErrorResult = { | |
75 | error: smartData.error, | |
76 | smartctl_error_code: smartData.smartctl_error_code, | |
77 | smartctl_output: smartData.smartctl_output, | |
78 | userMessage: userMessage, | |
79 | device: smartData.dev, | |
80 | identifier: smartData.nvme_vendor | |
81 | }; | |
82 | result[deviceId] = _result; | |
83 | return; | |
84 | } | |
85 | // Prepare S.M.A.R.T data | |
86 | if (smartData.json_format_version[0] === 1) { | |
87 | // Version 1.x | |
88 | if (this.isAtaSmartData(smartData)) { | |
89 | result[deviceId] = this.extractAtaData(smartData); | |
90 | } else if (this.isIscsiSmartData(smartData)) { | |
91 | result[deviceId] = this.extractIscsiData(smartData); | |
92 | } else if (this.isNvmeSmartData(smartData)) { | |
93 | result[deviceId] = this.extractNvmeData(smartData); | |
94 | } | |
95 | return; | |
96 | } else { | |
97 | this.incompatible = true; | |
98 | } | |
99 | }); | |
100 | this.data = result; | |
101 | this.loading = false; | |
102 | } | |
103 | ||
104 | private extractNvmeData(smartData: NvmeSmartDataV1): SmartDataResult { | |
105 | const info = _.omitBy(smartData, (_value: string, key: string) => | |
106 | ['nvme_smart_health_information_log'].includes(key) | |
107 | ); | |
108 | return { | |
109 | info: info, | |
110 | smart: { | |
111 | nvmeData: smartData.nvme_smart_health_information_log | |
112 | }, | |
113 | device: smartData.device.name, | |
114 | identifier: smartData.serial_number | |
115 | }; | |
116 | } | |
117 | ||
118 | private extractIscsiData(smartData: IscsiSmartDataV1): SmartDataResult { | |
119 | const info = _.omitBy(smartData, (_value: string, key: string) => | |
120 | ['scsi_error_counter_log', 'scsi_grown_defect_list'].includes(key) | |
121 | ); | |
122 | return { | |
123 | info: info, | |
124 | smart: { | |
125 | scsi_error_counter_log: smartData.scsi_error_counter_log, | |
126 | scsi_grown_defect_list: smartData.scsi_grown_defect_list | |
127 | }, | |
128 | device: info.device.name, | |
129 | identifier: info.serial_number | |
130 | }; | |
131 | } | |
132 | ||
133 | private extractAtaData(smartData: AtaSmartDataV1): SmartDataResult { | |
134 | const info = _.omitBy(smartData, (_value: string, key: string) => | |
135 | ['ata_smart_attributes', 'ata_smart_selective_self_test_log', 'ata_smart_data'].includes(key) | |
136 | ); | |
137 | return { | |
138 | info: info, | |
139 | smart: { | |
140 | attributes: smartData.ata_smart_attributes, | |
141 | data: smartData.ata_smart_data | |
142 | }, | |
143 | device: info.device.name, | |
144 | identifier: info.serial_number | |
145 | }; | |
146 | } | |
147 | ||
148 | private updateData() { | |
149 | this.loading = true; | |
150 | ||
151 | if (this.osdId !== null) { | |
152 | this.osdService.getSmartData(this.osdId).subscribe({ | |
153 | next: this.fetchData.bind(this), | |
154 | error: (error) => { | |
155 | error.preventDefault(); | |
156 | this.error = error; | |
157 | this.loading = false; | |
158 | } | |
159 | }); | |
160 | } else if (this.hostname !== null) { | |
161 | this.hostService.getSmartData(this.hostname).subscribe({ | |
162 | next: this.fetchData.bind(this), | |
163 | error: (error) => { | |
164 | error.preventDefault(); | |
165 | this.error = error; | |
166 | this.loading = false; | |
167 | } | |
168 | }); | |
169 | } | |
170 | } | |
171 | ||
172 | ngOnInit() { | |
173 | this.smartDataColumns = [ | |
174 | { prop: 'id', name: $localize`ID` }, | |
175 | { prop: 'name', name: $localize`Name` }, | |
176 | { prop: 'raw.value', name: $localize`Raw` }, | |
177 | { prop: 'thresh', name: $localize`Threshold` }, | |
178 | { prop: 'value', name: $localize`Value` }, | |
179 | { prop: 'when_failed', name: $localize`When Failed` }, | |
180 | { prop: 'worst', name: $localize`Worst` } | |
181 | ]; | |
182 | ||
183 | this.scsiSmartDataColumns = [ | |
184 | { | |
185 | prop: 'correction_algorithm_invocations', | |
186 | name: $localize`Correction Algorithm Invocations` | |
187 | }, | |
188 | { | |
189 | prop: 'errors_corrected_by_eccdelayed', | |
190 | name: $localize`Errors Corrected by ECC (Delayed)` | |
191 | }, | |
192 | { prop: 'errors_corrected_by_eccfast', name: $localize`Errors Corrected by ECC (Fast)` }, | |
193 | { | |
194 | prop: 'errors_corrected_by_rereads_rewrites', | |
195 | name: $localize`Errors Corrected by Rereads/Rewrites` | |
196 | }, | |
197 | { prop: 'gigabytes_processed', name: $localize`Gigabyes Processed` }, | |
198 | { prop: 'total_errors_corrected', name: $localize`Total Errors Corrected` }, | |
199 | { prop: 'total_uncorrected_errors', name: $localize`Total Errors Uncorrected` } | |
200 | ]; | |
201 | } | |
202 | ||
203 | ngOnChanges(changes: SimpleChanges): void { | |
204 | this.data = {}; // Clear previous data | |
205 | if (changes.osdId) { | |
206 | this.osdId = changes.osdId.currentValue; | |
207 | } else if (changes.hostname) { | |
208 | this.hostname = changes.hostname.currentValue; | |
209 | } | |
210 | this.updateData(); | |
211 | } | |
212 | } |