1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "utils/storage_radar.h"
17
18 #include "hisysevent.h"
19 #include "storage_service_errno.h"
20 #include "storage_service_log.h"
21 #include <chrono>
22 #include <sstream>
23 #include <iomanip>
24
25 namespace OHOS {
26 namespace StorageService {
ReportActiveUserKey(const std::string & funcName,uint32_t userId,int ret,const std::string & keyElxLevel)27 void StorageRadar::ReportActiveUserKey(const std::string &funcName, uint32_t userId, int ret,
28 const std::string &keyElxLevel)
29 {
30 RadarParameter param = {
31 .orgPkg = "account_mgr",
32 .userId = userId,
33 .funcName = funcName,
34 .bizScene = BizScene::USER_KEY_ENCRYPTION,
35 .bizStage = BizStage::BIZ_STAGE_ACTIVE_USER_KEY,
36 .keyElxLevel = keyElxLevel,
37 .errorCode = ret
38 };
39 StorageRadar::GetInstance().RecordFuctionResult(param);
40 }
41
ReportGetStorageStatus(const std::string & funcName,uint32_t userId,int ret,const std::string & orgPkg)42 void StorageRadar::ReportGetStorageStatus(const std::string &funcName, uint32_t userId, int ret,
43 const std::string &orgPkg)
44 {
45 RadarParameter param = {
46 .orgPkg = orgPkg,
47 .userId = userId,
48 .funcName = funcName,
49 .bizScene = BizScene::SPACE_STATISTICS,
50 .bizStage = BizStage::BIZ_STAGE_GET_USER_STORAGE_STATS,
51 .keyElxLevel = "NA",
52 .errorCode = ret
53 };
54 StorageRadar::GetInstance().RecordFuctionResult(param);
55 }
56
ReportVolumeOperation(const std::string & funcName,int ret)57 void StorageRadar::ReportVolumeOperation(const std::string &funcName, int ret)
58 {
59 RadarParameter param = {
60 .orgPkg = DEFAULT_ORGPKGNAME,
61 .userId = DEFAULT_USERID,
62 .funcName = funcName,
63 .bizScene = BizScene::EXTERNAL_VOLUME_MANAGER,
64 .bizStage = BizStage::BIZ_STAGE_MOUNT,
65 .keyElxLevel = "NA",
66 .errorCode = ret
67 };
68 StorageRadar::GetInstance().RecordFuctionResult(param);
69 }
70
ReportUserKeyResult(const std::string & funcName,uint32_t userId,int ret,const std::string & keyLevel,const std::string & extraData)71 void StorageRadar::ReportUserKeyResult(const std::string &funcName, uint32_t userId, int ret,
72 const std::string &keyLevel, const std::string &extraData)
73 {
74 RadarParameter param = {
75 .orgPkg = "init",
76 .userId = userId,
77 .funcName = funcName,
78 .bizScene = BizScene::USER_KEY_ENCRYPTION,
79 .bizStage = BizStage::BIZ_STAGE_INIT_GLOBAL_KEY,
80 .keyElxLevel = keyLevel,
81 .errorCode = ret,
82 .extraData = extraData
83 };
84 StorageRadar::GetInstance().RecordFuctionResult(param);
85 }
86
ReportUserManager(const std::string & funcName,uint32_t userId,int ret,enum BizStage stage)87 void StorageRadar::ReportUserManager(const std::string &funcName, uint32_t userId, int ret,
88 enum BizStage stage)
89 {
90 RadarParameter param = {
91 .orgPkg = "account_mgr",
92 .userId = userId,
93 .funcName = funcName,
94 .bizScene = BizScene::USER_MOUNT_MANAGER,
95 .bizStage = stage,
96 .keyElxLevel = "ELx",
97 .errorCode = ret
98 };
99 StorageRadar::GetInstance().RecordFuctionResult(param);
100 }
101
ReportUpdateUserAuth(const std::string & funcName,uint32_t userId,int ret,const std::string & keyLevel,const std::string & extraData)102 void StorageRadar::ReportUpdateUserAuth(const std::string &funcName, uint32_t userId, int ret,
103 const std::string &keyLevel, const std::string &extraData)
104 {
105 RadarParameter param = {
106 .orgPkg = "account_mgr",
107 .userId = userId,
108 .funcName = funcName,
109 .bizScene = BizScene::USER_KEY_ENCRYPTION,
110 .bizStage = BizStage::BIZ_STAGE_UPDATE_USER_AUTH,
111 .keyElxLevel = keyLevel,
112 .errorCode = ret,
113 .extraData = extraData
114 };
115 StorageRadar::GetInstance().RecordFuctionResult(param);
116 }
117
ReportFbexResult(const std::string & funcName,uint32_t userId,int ret,const std::string & keyLevel,const std::string & extraData)118 void StorageRadar::ReportFbexResult(const std::string &funcName, uint32_t userId, int ret,
119 const std::string &keyLevel, const std::string &extraData)
120 {
121 RadarParameter param = {
122 .orgPkg = "fbex",
123 .userId = userId,
124 .funcName = funcName,
125 .bizScene = BizScene::USER_KEY_ENCRYPTION,
126 .bizStage = BizStage::BIZ_STAGE_ACTIVE_USER_KEY,
127 .keyElxLevel = keyLevel,
128 .errorCode = ret,
129 .extraData = extraData
130 };
131 StorageRadar::GetInstance().RecordFuctionResult(param);
132 }
133
ReportIamResult(const std::string & funcName,uint32_t userId,int ret)134 void StorageRadar::ReportIamResult(const std::string &funcName, uint32_t userId, int ret)
135 {
136 RadarParameter param = {
137 .orgPkg = "iam",
138 .userId = userId,
139 .funcName = funcName,
140 .bizScene = BizScene::USER_KEY_ENCRYPTION,
141 .bizStage = BizStage::BIZ_STAGE_ACTIVE_USER_KEY,
142 .keyElxLevel = "NA",
143 .errorCode = ret
144 };
145 StorageRadar::GetInstance().RecordFuctionResult(param);
146 }
147
ReportHuksResult(const std::string & funcName,int ret)148 void StorageRadar::ReportHuksResult(const std::string &funcName, int ret)
149 {
150 RadarParameter param = {
151 .orgPkg = "huks",
152 .userId = DEFAULT_USERID,
153 .funcName = funcName,
154 .bizScene = BizScene::USER_KEY_ENCRYPTION,
155 .bizStage = BizStage::BIZ_STAGE_ACTIVE_USER_KEY,
156 .keyElxLevel = "NA",
157 .errorCode = ret
158 };
159 StorageRadar::GetInstance().RecordFuctionResult(param);
160 }
161
RecordKillProcessResult(std::string processName,int32_t errcode)162 bool StorageRadar::RecordKillProcessResult(std::string processName, int32_t errcode)
163 {
164 int32_t res = E_OK;
165 if (errcode == E_OK) {
166 res = HiSysEventWrite(
167 STORAGESERVICE_DOAMIN,
168 UMOUNT_FAIL_BEHAVIOR,
169 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
170 "ORG_PKG", DEFAULT_ORGPKGNAME,
171 "FUNC", "FindAndKillProcess",
172 "BIZ_SCENE", static_cast<int32_t>(BizScene::STORAGE_START),
173 "BIZ_STAGE", static_cast<int32_t>(BizStage::BIZ_STAGE_SA_START),
174 "STAGE_RES", static_cast<int32_t>(StageRes::STAGE_SUCC),
175 "BIZ_STATE", static_cast<int32_t>(BizState::BIZ_STATE_END));
176 } else {
177 res = HiSysEventWrite(
178 STORAGESERVICE_DOAMIN,
179 UMOUNT_FAIL_BEHAVIOR,
180 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
181 "ORG_PKG", DEFAULT_ORGPKGNAME,
182 "FUNC", "FindAndKillProcess",
183 "BIZ_SCENE", static_cast<int32_t>(BizScene::STORAGE_START),
184 "BIZ_STAGE", static_cast<int32_t>(BizStage::BIZ_STAGE_SA_START),
185 "STAGE_RES", static_cast<int32_t>(StageRes::STAGE_FAIL),
186 "BIZ_STATE", static_cast<int32_t>(BizState::BIZ_STATE_END),
187 "ERROR_CODE", errcode,
188 "PROCESS_NAME", processName);
189 }
190 if (res != E_OK) {
191 LOGE("StorageRadar ERROR, res :%{public}d", res);
192 return false;
193 }
194 return true;
195 }
196
RecordFuctionResult(const RadarParameter & parRes)197 bool StorageRadar::RecordFuctionResult(const RadarParameter &parRes)
198 {
199 int32_t res = E_OK;
200 if (parRes.errorCode == E_OK) {
201 res = HiSysEventWrite(
202 STORAGESERVICE_DOAMIN,
203 FILE_STORAGE_MANAGER_FAULT_BEHAVIOR,
204 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
205 "ORG_PKG", parRes.orgPkg,
206 "USER_ID", parRes.userId,
207 "FUNC", parRes.funcName,
208 "BIZ_SCENE", static_cast<int32_t>(parRes.bizScene),
209 "BIZ_STAGE", static_cast<int32_t>(parRes.bizStage),
210 "kEY_ELX_LEVEL", parRes.keyElxLevel,
211 "TO_CALL_PKG", "FBEX, HUKS, KEY_RING, BUNDLE_MANAGER, HMDFS",
212 "DISK_VOLUME_INFO", "{\"diskId\":\"ab12\", \"volumeId\":\"34cd\", \"fsType\":\"ntfs\"}",
213 "FILE_STATUS", parRes.extraData,
214 "STAGE_RES", static_cast<int32_t>(StageRes::STAGE_SUCC),
215 "BIZ_STATE", static_cast<int32_t>(BizState::BIZ_STATE_START));
216 } else {
217 res = HiSysEventWrite(
218 STORAGESERVICE_DOAMIN,
219 FILE_STORAGE_MANAGER_FAULT_BEHAVIOR,
220 HiviewDFX::HiSysEvent::EventType::FAULT,
221 "ORG_PKG", parRes.orgPkg,
222 "USER_ID", parRes.userId,
223 "FUNC", parRes.funcName,
224 "BIZ_SCENE", static_cast<int32_t>(parRes.bizScene),
225 "BIZ_STAGE", static_cast<int32_t>(parRes.bizStage),
226 "kEY_ELX_LEVEL", parRes.keyElxLevel,
227 "TO_CALL_PKG", "FBEX, HUKS, KEY_RING, BUNDLE_MANAGER, HMDFS",
228 "DISK_VOLUME_INFO", "{\"diskId\":\"ab12\", \"volumeId\":\"34cd\", \"fsType\":\"ntfs\"}",
229 "FILE_STATUS", parRes.extraData,
230 "STAGE_RES", static_cast<int32_t>(StageRes::STAGE_FAIL),
231 "BIZ_STATE", static_cast<int32_t>(BizState::BIZ_STATE_START),
232 "ERROR_CODE", parRes.errorCode);
233 }
234 if (res != E_OK) {
235 LOGE("StorageRadar ERROR, res :%{public}d", res);
236 return false;
237 }
238 return true;
239 }
240
GetCurrentTime()241 static std::string GetCurrentTime()
242 {
243 auto now = std::chrono::system_clock::now();
244 auto time = std::chrono::system_clock::to_time_t(now);
245 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
246 std::stringstream strTime;
247 strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0'))
248 << (std::setw(INDEX)) << (ms.count() % MS_1000);
249 return strTime.str();
250 }
251
ReportStatistics(uint32_t userId,StorageDaemon::RadarStatisticInfo radarInfo)252 void StorageRadar::ReportStatistics(uint32_t userId, StorageDaemon::RadarStatisticInfo radarInfo)
253 {
254 int32_t res = HiSysEventWrite(
255 STORAGESERVICE_DOAMIN,
256 FILE_STORAGE_MANAGER_STATISTIC,
257 HiviewDFX::HiSysEvent::EventType::STATISTIC,
258 "USER_ID", userId,
259 "TIME", GetCurrentTime(),
260 "KEY_LOAD_SUCC_CNT", radarInfo.keyLoadSuccCount,
261 "KEY_LOAD_FAIL_CNT", radarInfo.keyLoadFailCount,
262 "KEY_UNLOAD_SUCC_CNT", radarInfo.keyUnloadSuccCount,
263 "KEY_UNLOAD_FAIL_CNT", radarInfo.keyUnloadFailCount,
264 "USER_ADD_SUCC_CNT", radarInfo.userAddSuccCount,
265 "USER_ADD_FAIL_CNT", radarInfo.userAddFailCount,
266 "USER_REMOVE_SUCC_CNT", radarInfo.userRemoveSuccCount,
267 "USER_REMOVE_FAIL_CNT", radarInfo.userRemoveFailCount,
268 "USER_START_SUCC_CNT", radarInfo.userStartSuccCount,
269 "USER_START_FAIL_CNT", radarInfo.userStartFailCount,
270 "USER_STOP_SUCC_CNT", radarInfo.userStopSuccCount,
271 "USER_STOP_FAIL_CNT", radarInfo.userStopFailCount);
272 if (res != E_OK) {
273 LOGE("StorageRadar ERROR, res :%{public}d", res);
274 }
275 }
276 } // namespace StorageService
277 } // namespace OHOS