PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/druid/support/http/stat/WebAppStat.java

https://gitlab.com/lizhanhui/druid
Java | 1179 lines | 936 code | 222 blank | 21 comment | 240 complexity | 435601faec2c4123d474baed8c39cdc5 MD5 | raw file
  1. /*
  2. * Copyright 1999-2011 Alibaba Group Holding Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.druid.support.http.stat;
  17. import com.alibaba.druid.support.logging.Log;
  18. import com.alibaba.druid.support.logging.LogFactory;
  19. import com.alibaba.druid.util.LRUCache;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import java.util.concurrent.ConcurrentMap;
  27. import java.util.concurrent.atomic.AtomicInteger;
  28. import java.util.concurrent.atomic.AtomicLong;
  29. import java.util.concurrent.locks.ReadWriteLock;
  30. import java.util.concurrent.locks.ReentrantReadWriteLock;
  31. import static com.alibaba.druid.util.JdbcSqlStatUtils.get;
  32. public class WebAppStat {
  33. private final static Log LOG = LogFactory.getLog(WebAppStat.class);
  34. public final static int DEFAULT_MAX_STAT_URI_COUNT = 1000;
  35. public final static int DEFAULT_MAX_STAT_SESSION_COUNT = 1000;
  36. private final static ThreadLocal<WebAppStat> currentLocal = new ThreadLocal<WebAppStat>();
  37. private volatile int maxStatUriCount = DEFAULT_MAX_STAT_URI_COUNT;
  38. private volatile int maxStatSessionCount = DEFAULT_MAX_STAT_SESSION_COUNT;
  39. private final AtomicInteger runningCount = new AtomicInteger();
  40. private final AtomicInteger concurrentMax = new AtomicInteger();
  41. private final AtomicLong requestCount = new AtomicLong(0);
  42. private final AtomicLong sessionCount = new AtomicLong(0);
  43. private final AtomicLong jdbcFetchRowCount = new AtomicLong();
  44. private final AtomicLong jdbcUpdateCount = new AtomicLong();
  45. private final AtomicLong jdbcExecuteCount = new AtomicLong();
  46. private final AtomicLong jdbcExecuteTimeNano = new AtomicLong();
  47. private final AtomicLong jdbcCommitCount = new AtomicLong();
  48. private final AtomicLong jdbcRollbackCount = new AtomicLong();
  49. private final ConcurrentMap<String, WebURIStat> uriStatMap = new ConcurrentHashMap<String, WebURIStat>(
  50. 16,
  51. 0.75f,
  52. 1);
  53. private final LRUCache<String, WebSessionStat> sessionStatMap;
  54. private final ReadWriteLock sessionStatLock = new ReentrantReadWriteLock();
  55. private final AtomicLong uriStatMapFullCount = new AtomicLong();
  56. private final AtomicLong uriSessionMapFullCount = new AtomicLong();
  57. private final AtomicLong osMacOSXCount = new AtomicLong(0);
  58. private final AtomicLong osWindowsCount = new AtomicLong(0);
  59. private final AtomicLong osLinuxCount = new AtomicLong(0);
  60. private final AtomicLong osSymbianCount = new AtomicLong(0);
  61. private final AtomicLong osFreeBSDCount = new AtomicLong(0);
  62. private final AtomicLong osOpenBSDCount = new AtomicLong(0);
  63. private final AtomicLong osAndroidCount = new AtomicLong(0);
  64. private final AtomicLong osWindows98Count = new AtomicLong();
  65. private final AtomicLong osWindowsXPCount = new AtomicLong();
  66. private final AtomicLong osWindows2000Count = new AtomicLong();
  67. private final AtomicLong osWindowsVistaCount = new AtomicLong();
  68. private final AtomicLong osWindows7Count = new AtomicLong();
  69. private final AtomicLong osWindows8Count = new AtomicLong();
  70. private final AtomicLong osAndroid15Count = new AtomicLong(0);
  71. private final AtomicLong osAndroid16Count = new AtomicLong(0);
  72. private final AtomicLong osAndroid20Count = new AtomicLong(0);
  73. private final AtomicLong osAndroid21Count = new AtomicLong(0);
  74. private final AtomicLong osAndroid22Count = new AtomicLong(0);
  75. private final AtomicLong osAndroid23Count = new AtomicLong(0);
  76. private final AtomicLong osAndroid30Count = new AtomicLong(0);
  77. private final AtomicLong osAndroid31Count = new AtomicLong(0);
  78. private final AtomicLong osAndroid32Count = new AtomicLong(0);
  79. private final AtomicLong osAndroid40Count = new AtomicLong(0);
  80. private final AtomicLong osAndroid41Count = new AtomicLong(0);
  81. private final AtomicLong osAndroid42Count = new AtomicLong(0);
  82. private final AtomicLong osAndroid43Count = new AtomicLong(0);
  83. private final AtomicLong osLinuxUbuntuCount = new AtomicLong(0);
  84. private final AtomicLong browserIECount = new AtomicLong(0);
  85. private final AtomicLong browserFirefoxCount = new AtomicLong(0);
  86. private final AtomicLong browserChromeCount = new AtomicLong(0);
  87. private final AtomicLong browserSafariCount = new AtomicLong(0);
  88. private final AtomicLong browserOperaCount = new AtomicLong(0);
  89. private final AtomicLong browserIE5Count = new AtomicLong(0);
  90. private final AtomicLong browserIE6Count = new AtomicLong(0);
  91. private final AtomicLong browserIE7Count = new AtomicLong(0);
  92. private final AtomicLong browserIE8Count = new AtomicLong(0);
  93. private final AtomicLong browserIE9Count = new AtomicLong(0);
  94. private final AtomicLong browserIE10Count = new AtomicLong(0);
  95. private final AtomicLong browser360SECount = new AtomicLong(0);
  96. private final AtomicLong deviceAndroidCount = new AtomicLong(0);
  97. private final AtomicLong deviceIpadCount = new AtomicLong(0);
  98. private final AtomicLong deviceIphoneCount = new AtomicLong(0);
  99. private final AtomicLong deviceWindowsPhoneCount = new AtomicLong(0);
  100. private final AtomicLong botCount = new AtomicLong();
  101. private final AtomicLong botBaiduCount = new AtomicLong();
  102. private final AtomicLong botYoudaoCount = new AtomicLong();
  103. private final AtomicLong botGoogleCount = new AtomicLong();
  104. private final AtomicLong botMsnCount = new AtomicLong();
  105. private final AtomicLong botBingCount = new AtomicLong();
  106. private final AtomicLong botSosoCount = new AtomicLong();
  107. private final AtomicLong botSogouCount = new AtomicLong();
  108. private final AtomicLong botYahooCount = new AtomicLong();
  109. private String contextPath;
  110. public static WebAppStat current() {
  111. return currentLocal.get();
  112. }
  113. public void reset() {
  114. concurrentMax.set(0);
  115. requestCount.set(0);
  116. requestCount.set(0);
  117. sessionCount.set(0);
  118. jdbcFetchRowCount.set(0);
  119. jdbcUpdateCount.set(0);
  120. jdbcExecuteCount.set(0);
  121. jdbcExecuteTimeNano.set(0);
  122. jdbcCommitCount.set(0);
  123. jdbcRollbackCount.set(0);
  124. sessionStatLock.readLock().lock();
  125. try {
  126. Iterator<Map.Entry<String, WebSessionStat>> iter = sessionStatMap.entrySet().iterator();
  127. while (iter.hasNext()) {
  128. Map.Entry<String, WebSessionStat> entry = iter.next();
  129. entry.getValue().reset();
  130. }
  131. sessionStatMap.clear();
  132. } finally {
  133. sessionStatLock.readLock().unlock();
  134. }
  135. uriStatMap.clear();
  136. uriStatMapFullCount.set(0);
  137. uriSessionMapFullCount.set(0);
  138. osMacOSXCount.set(0);
  139. osWindowsCount.set(0);
  140. osLinuxCount.set(0);
  141. osSymbianCount.set(0);
  142. osOpenBSDCount.set(0);
  143. osFreeBSDCount.set(0);
  144. osAndroidCount.set(0);
  145. osWindows98Count.set(0);
  146. osWindowsXPCount.set(0);
  147. osWindows2000Count.set(0);
  148. osWindowsVistaCount.set(0);
  149. osWindows7Count.set(0);
  150. osWindows8Count.set(0);
  151. osLinuxUbuntuCount.set(0);
  152. osAndroid15Count.set(0);
  153. osAndroid16Count.set(0);
  154. osAndroid20Count.set(0);
  155. osAndroid21Count.set(0);
  156. osAndroid22Count.set(0);
  157. osAndroid23Count.set(0);
  158. osAndroid30Count.set(0);
  159. osAndroid31Count.set(0);
  160. osAndroid32Count.set(0);
  161. osAndroid40Count.set(0);
  162. osAndroid41Count.set(0);
  163. osAndroid42Count.set(0);
  164. osAndroid43Count.set(0);
  165. browserIE6Count.set(0);
  166. browserIE7Count.set(0);
  167. browserIE8Count.set(0);
  168. browserIE9Count.set(0);
  169. browserIE10Count.set(0);
  170. browserIECount.set(0);
  171. browserFirefoxCount.set(0);
  172. browserChromeCount.set(0);
  173. browserSafariCount.set(0);
  174. browserOperaCount.set(0);
  175. browser360SECount.set(0);
  176. deviceAndroidCount.set(0);
  177. deviceIpadCount.set(0);
  178. deviceIphoneCount.set(0);
  179. deviceWindowsPhoneCount.set(0);
  180. }
  181. public WebAppStat(){
  182. this(null);
  183. }
  184. public WebAppStat(String contextPath){
  185. this(contextPath, DEFAULT_MAX_STAT_SESSION_COUNT);
  186. }
  187. public WebAppStat(String contextPath, int maxStatSessionCount){
  188. this.contextPath = contextPath;
  189. this.maxStatSessionCount = maxStatSessionCount;
  190. sessionStatMap = new LRUCache<String, WebSessionStat>(maxStatSessionCount);
  191. }
  192. public String getContextPath() {
  193. return contextPath;
  194. }
  195. public void beforeInvoke() {
  196. currentLocal.set(this);
  197. int running = runningCount.incrementAndGet();
  198. for (;;) {
  199. int max = concurrentMax.get();
  200. if (running > max) {
  201. if (concurrentMax.compareAndSet(max, running)) {
  202. break;
  203. }
  204. } else {
  205. break;
  206. }
  207. }
  208. requestCount.incrementAndGet();
  209. }
  210. public WebURIStat getURIStat(String uri) {
  211. return getURIStat(uri, false);
  212. }
  213. public WebURIStat getURIStat(String uri, boolean create) {
  214. WebURIStat uriStat = uriStatMap.get(uri);
  215. if (uriStat != null) {
  216. return uriStat;
  217. }
  218. if (!create) {
  219. return null;
  220. }
  221. if (uriStatMap.size() >= this.getMaxStatUriCount()) {
  222. long fullCount = uriStatMapFullCount.getAndIncrement();
  223. if (fullCount == 0) {
  224. LOG.error("uriSessionMapFullCount is full");
  225. }
  226. return null;
  227. }
  228. uriStatMap.putIfAbsent(uri, new WebURIStat(uri));
  229. uriStat = uriStatMap.get(uri);
  230. return uriStat;
  231. }
  232. public WebSessionStat getSessionStat(String sessionId) {
  233. return getSessionStat(sessionId, false);
  234. }
  235. public Map<String, Object> getSessionStatData(String sessionId) {
  236. WebSessionStat sessionStat = sessionStatMap.get(sessionId);
  237. if (sessionStat == null) {
  238. return null;
  239. }
  240. return sessionStat.getStatData();
  241. }
  242. public Map<String, Object> getURIStatData(String uri) {
  243. WebURIStat uriStat = getURIStat(uri);
  244. if (uriStat == null) {
  245. return null;
  246. }
  247. return uriStat.getStatData();
  248. }
  249. public WebSessionStat getSessionStat(String sessionId, boolean create) {
  250. sessionStatLock.readLock().lock();
  251. try {
  252. WebSessionStat uriStat = sessionStatMap.get(sessionId);
  253. if (uriStat != null) {
  254. return uriStat;
  255. }
  256. } finally {
  257. sessionStatLock.readLock().unlock();
  258. }
  259. if (!create) {
  260. return null;
  261. }
  262. sessionStatLock.writeLock().lock();
  263. try {
  264. WebSessionStat uriStat = sessionStatMap.get(sessionId);
  265. if (uriStat == null) {
  266. if (sessionStatMap.size() >= this.getMaxStatSessionCount()) {
  267. long fullCount = uriSessionMapFullCount.getAndIncrement();
  268. if (fullCount == 0) {
  269. LOG.error("sessionStatMap is full");
  270. }
  271. }
  272. WebSessionStat newStat = new WebSessionStat(sessionId);
  273. sessionStatMap.put(sessionId, newStat);
  274. return newStat;
  275. }
  276. return uriStat;
  277. } finally {
  278. sessionStatLock.writeLock().unlock();
  279. }
  280. }
  281. public void afterInvoke(Throwable error, long nanoSpan) {
  282. runningCount.decrementAndGet();
  283. currentLocal.set(null);
  284. WebRequestStat requestStat = WebRequestStat.current();
  285. if (requestStat != null) {
  286. this.addJdbcExecuteCount(requestStat.getJdbcExecuteCount());
  287. this.addJdbcFetchRowCount(requestStat.getJdbcFetchRowCount());
  288. this.addJdbcUpdateCount(requestStat.getJdbcUpdateCount());
  289. this.addJdbcCommitCount(requestStat.getJdbcCommitCount());
  290. this.addJdbcRollbackCount(requestStat.getJdbcRollbackCount());
  291. this.addJdbcExecuteTimeNano(requestStat.getJdbcExecuteTimeNano());
  292. }
  293. }
  294. public void incrementSessionCount() {
  295. sessionCount.incrementAndGet();
  296. }
  297. public long getSessionCount() {
  298. return sessionCount.get();
  299. }
  300. public void addJdbcFetchRowCount(long delta) {
  301. this.jdbcFetchRowCount.addAndGet(delta);
  302. }
  303. public long getJdbcFetchRowCount() {
  304. return jdbcFetchRowCount.get();
  305. }
  306. public void addJdbcUpdateCount(long updateCount) {
  307. this.jdbcUpdateCount.addAndGet(updateCount);
  308. }
  309. public long getJdbcUpdateCount() {
  310. return jdbcUpdateCount.get();
  311. }
  312. public void incrementJdbcExecuteCount() {
  313. jdbcExecuteCount.incrementAndGet();
  314. }
  315. public void addJdbcExecuteCount(long executeCount) {
  316. jdbcExecuteCount.addAndGet(executeCount);
  317. }
  318. public long getJdbcExecuteCount() {
  319. return jdbcExecuteCount.get();
  320. }
  321. public long getJdbcExecuteTimeNano() {
  322. return jdbcExecuteTimeNano.get();
  323. }
  324. public void addJdbcExecuteTimeNano(long nano) {
  325. jdbcExecuteTimeNano.addAndGet(nano);
  326. }
  327. public void incrementJdbcCommitCount() {
  328. jdbcCommitCount.incrementAndGet();
  329. }
  330. public long getJdbcCommitCount() {
  331. return jdbcCommitCount.get();
  332. }
  333. public void addJdbcCommitCount(long commitCount) {
  334. this.jdbcCommitCount.addAndGet(commitCount);
  335. }
  336. public void incrementJdbcRollbackCount() {
  337. jdbcRollbackCount.incrementAndGet();
  338. }
  339. public long getJdbcRollbackCount() {
  340. return jdbcRollbackCount.get();
  341. }
  342. public void addJdbcRollbackCount(long rollbackCount) {
  343. this.jdbcRollbackCount.addAndGet(rollbackCount);
  344. }
  345. public int getMaxStatUriCount() {
  346. return maxStatUriCount;
  347. }
  348. public void setMaxStatUriCount(int maxStatUriCount) {
  349. this.maxStatUriCount = maxStatUriCount;
  350. }
  351. public int getMaxStatSessionCount() {
  352. return maxStatSessionCount;
  353. }
  354. public void setMaxStatSessionCount(int maxStatSessionCount) {
  355. this.maxStatSessionCount = maxStatSessionCount;
  356. }
  357. public int getRunningCount() {
  358. return this.runningCount.get();
  359. }
  360. public long getConcurrentMax() {
  361. return concurrentMax.get();
  362. }
  363. public long getRequestCount() {
  364. return requestCount.get();
  365. }
  366. public Map<String, Object> getStatData() {
  367. return getStatValue(false).getStatData();
  368. }
  369. public List<WebURIStatValue> getURIStatValueList(boolean reset) {
  370. List<WebURIStatValue> list = new ArrayList<WebURIStatValue>(this.uriStatMap.size());
  371. for (WebURIStat uriStat : this.uriStatMap.values()) {
  372. WebURIStatValue statValue = uriStat.getValue(reset);
  373. if (statValue.getRunningCount() == 0 && statValue.getRequestCount() == 0) {
  374. continue;
  375. }
  376. list.add(statValue);
  377. }
  378. return list;
  379. }
  380. public List<Map<String, Object>> getURIStatDataList() {
  381. List<Map<String, Object>> uriStatDataList = new ArrayList<Map<String, Object>>(this.uriStatMap.size());
  382. for (WebURIStat uriStat : this.uriStatMap.values()) {
  383. Map<String, Object> uriStatData = uriStat.getStatData();
  384. int runningCount = ((Number) uriStatData.get("RunningCount")).intValue();
  385. long requestCount = (Long) uriStatData.get("RequestCount");
  386. if (runningCount == 0 && requestCount == 0) {
  387. continue;
  388. }
  389. uriStatDataList.add(uriStatData);
  390. }
  391. return uriStatDataList;
  392. }
  393. public List<Map<String, Object>> getSessionStatDataList() {
  394. List<Map<String, Object>> sessionStatDataList = new ArrayList<Map<String, Object>>(this.sessionStatMap.size());
  395. for (WebSessionStat sessionStat : Collections.unmodifiableCollection(this.sessionStatMap.values())) {
  396. Map<String, Object> sessionStatData = sessionStat.getStatData();
  397. int runningCount = ((Number) sessionStatData.get("RunningCount")).intValue();
  398. long requestCount = (Long) sessionStatData.get("RequestCount");
  399. if (runningCount == 0 && requestCount == 0) {
  400. continue;
  401. }
  402. sessionStatDataList.add(sessionStatData);
  403. }
  404. return sessionStatDataList;
  405. }
  406. public void computeUserAgent(String userAgent) {
  407. if (userAgent == null || userAgent.length() == 0) {
  408. return;
  409. }
  410. // Mozilla/5.0 (compatible;
  411. final int MOZILLA_COMPATIBLE_OFFSET = 25;
  412. boolean is360SE = userAgent.endsWith("360SE)");
  413. if (is360SE) {
  414. browser360SECount.incrementAndGet();
  415. }
  416. boolean isIE = userAgent.startsWith("MSIE", MOZILLA_COMPATIBLE_OFFSET);
  417. int iePrefixIndex = 30; // "Mozilla/5.0 (compatible; MSIE ".length();
  418. boolean isGoogleToolbar = false;
  419. if (!isIE) {
  420. isGoogleToolbar = userAgent.startsWith("GoogleToolbar", MOZILLA_COMPATIBLE_OFFSET);
  421. if (isGoogleToolbar) {
  422. // MSIE
  423. int tmp = userAgent.indexOf("IE ");
  424. if (tmp != -1) {
  425. isIE = true;
  426. iePrefixIndex = tmp + 3;
  427. }
  428. }
  429. }
  430. if (isIE) {
  431. browserIECount.incrementAndGet();
  432. char v1 = ' ', v2 = ' ';
  433. if (userAgent.length() > iePrefixIndex + 1) {
  434. v1 = userAgent.charAt(iePrefixIndex);
  435. v2 = userAgent.charAt(iePrefixIndex + 1);
  436. } else if (userAgent.length() > iePrefixIndex) {
  437. v1 = userAgent.charAt(iePrefixIndex);
  438. }
  439. switch (v1) {
  440. case '5':
  441. browserIE5Count.incrementAndGet();
  442. break;
  443. case '6':
  444. browserIE6Count.incrementAndGet();
  445. break;
  446. case '7':
  447. browserIE7Count.incrementAndGet();
  448. break;
  449. case '8':
  450. browserIE8Count.incrementAndGet();
  451. break;
  452. case '9':
  453. browserIE9Count.incrementAndGet();
  454. break;
  455. case '1':
  456. if (v2 == '0') {
  457. browserIE10Count.incrementAndGet();
  458. }
  459. break;
  460. default:
  461. break;
  462. }
  463. osWindowsCount.incrementAndGet();
  464. computeUserAgentIEWindowsVersion(userAgent);
  465. if (userAgent.contains("Windows Phone")) {
  466. deviceWindowsPhoneCount.incrementAndGet();
  467. }
  468. return;
  469. }
  470. boolean isWindows = false;
  471. boolean isMac = false;
  472. boolean isIpad = false;
  473. boolean isIPhone = false;
  474. boolean isLinux = false;
  475. boolean isX11 = false;
  476. boolean isBSD = false;
  477. if (userAgent.startsWith("Windows", 13)) {
  478. isWindows = true;
  479. } else if (userAgent.startsWith("Macintosh", 13)) {
  480. isMac = true;
  481. } else if (userAgent.startsWith("iPad", 13)) {
  482. isIpad = true;
  483. isMac = true;
  484. } else if (userAgent.startsWith("iPhone", 13)) {
  485. isIPhone = true;
  486. isMac = true;
  487. } else if (userAgent.startsWith("Linux", 13)) {
  488. isLinux = true;
  489. } else if (userAgent.startsWith("X11", 13)) {
  490. isX11 = true;
  491. }
  492. boolean isAndroid = false;
  493. if (isWindows) {
  494. isWindows = true;
  495. osWindowsCount.incrementAndGet();
  496. if (userAgent.contains("Windows Phone")) {
  497. deviceWindowsPhoneCount.incrementAndGet();
  498. }
  499. } else if (isMac) {
  500. isMac = true;
  501. osMacOSXCount.incrementAndGet();
  502. if (isIpad && userAgent.contains("iPad")) {
  503. deviceIpadCount.incrementAndGet();
  504. } else if (isIPhone || userAgent.contains("iPhone")) {
  505. deviceIphoneCount.incrementAndGet();
  506. }
  507. } else if (isLinux) {
  508. osLinuxCount.incrementAndGet();
  509. isAndroid = computeUserAgentAndroid(userAgent);
  510. } else if (userAgent.contains("Symbian")) {
  511. osSymbianCount.incrementAndGet();
  512. } else if (userAgent.contains("Ubuntu")) {
  513. osLinuxCount.incrementAndGet();
  514. osLinuxUbuntuCount.incrementAndGet();
  515. isLinux = true;
  516. }
  517. if (isX11) {
  518. if (userAgent.contains("OpenBSD")) {
  519. osOpenBSDCount.incrementAndGet();
  520. isBSD = true;
  521. } else if (userAgent.contains("FreeBSD")) {
  522. osFreeBSDCount.incrementAndGet();
  523. isBSD = true;
  524. } else if ((!isLinux) && userAgent.contains("Linux")) {
  525. osLinuxCount.incrementAndGet();
  526. isLinux = true;
  527. }
  528. }
  529. boolean isOpera = userAgent.startsWith("Opera");
  530. if (isOpera) {
  531. if (userAgent.contains("Windows")) {
  532. osWindowsCount.incrementAndGet();
  533. } else if (userAgent.contains("Linux")) {
  534. osWindowsCount.incrementAndGet();
  535. } else if (userAgent.contains("Macintosh")) {
  536. osMacOSXCount.incrementAndGet();
  537. }
  538. browserOperaCount.incrementAndGet();
  539. return;
  540. }
  541. if (isWindows) {
  542. computeUserAgentFirefoxWindowsVersion(userAgent);
  543. }
  544. if (isWindows || isMac || isLinux || isBSD) {
  545. if (userAgent.contains("Chrome")) {
  546. browserChromeCount.incrementAndGet();
  547. return;
  548. }
  549. if ((!isAndroid) && userAgent.contains("Safari")) {
  550. browserSafariCount.incrementAndGet();
  551. return;
  552. }
  553. if (userAgent.contains("Firefox")) {
  554. browserFirefoxCount.incrementAndGet();
  555. return;
  556. }
  557. }
  558. if (userAgent.startsWith("User-Agent: ")) {
  559. String rest = userAgent.substring("User-Agent: ".length());
  560. computeUserAgent(rest);
  561. }
  562. boolean isJava = userAgent.startsWith("Java");
  563. if (isJava) {
  564. botCount.incrementAndGet();
  565. }
  566. if (userAgent.startsWith("msnbot")) {
  567. botCount.incrementAndGet();
  568. botMsnCount.incrementAndGet();
  569. } else if (userAgent.startsWith("Sosospider+")) {
  570. botCount.incrementAndGet();
  571. botSosoCount.incrementAndGet();
  572. } else if (userAgent.startsWith("Sogou")) {
  573. botCount.incrementAndGet();
  574. botSogouCount.incrementAndGet();
  575. } else if (userAgent.startsWith("HuaweiSymantecSpider")) {
  576. botCount.incrementAndGet();
  577. } else if (userAgent.startsWith("Yeti/")) {
  578. botCount.incrementAndGet();
  579. } else if (userAgent.startsWith("mahonie")) {
  580. botCount.incrementAndGet();
  581. } else if (userAgent.startsWith("findlinks")) {
  582. botCount.incrementAndGet();
  583. } else if (userAgent.startsWith("Updownerbot")) {
  584. botCount.incrementAndGet();
  585. } else if (userAgent.startsWith("DoCoMo/")) {
  586. botCount.incrementAndGet();
  587. } else if (userAgent.startsWith("Crawl")) {
  588. botCount.incrementAndGet();
  589. } else if (userAgent.startsWith("SkimBot")) {
  590. botCount.incrementAndGet();
  591. } else if (userAgent.startsWith("YoudaoBot", MOZILLA_COMPATIBLE_OFFSET)) {
  592. botCount.incrementAndGet();
  593. botYoudaoCount.incrementAndGet();
  594. } else if (userAgent.startsWith("bingbot", MOZILLA_COMPATIBLE_OFFSET)) {
  595. botCount.incrementAndGet();
  596. botBingCount.incrementAndGet();
  597. } else if (userAgent.startsWith("Googlebot", MOZILLA_COMPATIBLE_OFFSET)) {
  598. botCount.incrementAndGet();
  599. botGoogleCount.incrementAndGet();
  600. } else if (userAgent.startsWith("Baiduspider", MOZILLA_COMPATIBLE_OFFSET)) {
  601. botCount.incrementAndGet();
  602. botBaiduCount.incrementAndGet();
  603. } else if (userAgent.startsWith("MJ12bot", MOZILLA_COMPATIBLE_OFFSET)) {
  604. botCount.incrementAndGet();
  605. botBaiduCount.incrementAndGet();
  606. } else if (userAgent.startsWith("Mail.RU/", MOZILLA_COMPATIBLE_OFFSET)) {
  607. botCount.incrementAndGet();
  608. } else if (userAgent.startsWith("Yahoo!", MOZILLA_COMPATIBLE_OFFSET)) {
  609. botCount.incrementAndGet();
  610. botYahooCount.incrementAndGet();
  611. } else if (userAgent.startsWith("KaloogaBot", MOZILLA_COMPATIBLE_OFFSET)) {
  612. botCount.incrementAndGet();
  613. } else if (userAgent.startsWith("YandexBot", MOZILLA_COMPATIBLE_OFFSET)) {
  614. botCount.incrementAndGet();
  615. } else if (userAgent.startsWith("Ezooms/", MOZILLA_COMPATIBLE_OFFSET)) {
  616. botCount.incrementAndGet();
  617. } else if (userAgent.startsWith("Exabot/", MOZILLA_COMPATIBLE_OFFSET)) {
  618. botCount.incrementAndGet();
  619. } else if (userAgent.startsWith("AhrefsBot/", MOZILLA_COMPATIBLE_OFFSET)) {
  620. botCount.incrementAndGet();
  621. } else if (userAgent.startsWith("YodaoBot/", MOZILLA_COMPATIBLE_OFFSET)) {
  622. botCount.incrementAndGet();
  623. } else if (userAgent.startsWith("BeetleBot", MOZILLA_COMPATIBLE_OFFSET)) {
  624. botCount.incrementAndGet();
  625. } else if (userAgent.startsWith("archive.org_bot", MOZILLA_COMPATIBLE_OFFSET)) {
  626. botCount.incrementAndGet();
  627. } else if (userAgent.startsWith("aiHitBot", MOZILLA_COMPATIBLE_OFFSET)) {
  628. botCount.incrementAndGet();
  629. } else if (userAgent.startsWith("EventGuruBot", MOZILLA_COMPATIBLE_OFFSET)) {
  630. botCount.incrementAndGet();
  631. } else if (userAgent.equals("Mozilla/5.0 ()")) {
  632. botCount.incrementAndGet();
  633. } else if (userAgent.equals("\"Mozilla/5.0")) {
  634. botCount.incrementAndGet();
  635. } else if (userAgent.equals("Mozilla")) {
  636. botCount.incrementAndGet();
  637. } else if (userAgent.equals("-")) {
  638. botCount.incrementAndGet();
  639. } else if (userAgent.contains("Spider") || userAgent.contains("spider")) {
  640. botCount.incrementAndGet();
  641. } else if (userAgent.contains("crawl") || userAgent.contains("Crawl")) {
  642. botCount.incrementAndGet();
  643. } else if (userAgent.contains("Bot") || userAgent.contains("bot")) {
  644. botCount.incrementAndGet();
  645. }
  646. // Mozilla/5.0 ()
  647. // Mozilla/5.0 (compatible; Mail.RU/2.0)
  648. // Mozilla/5.0 (compatible; bingbot/2.0;
  649. // YoudaoBot
  650. }
  651. private void computeUserAgentFirefoxWindowsVersion(String userAgent) {
  652. if (userAgent.startsWith("Windows NT 5.1", 13)) {
  653. osWindowsXPCount.incrementAndGet();
  654. } else if (userAgent.startsWith("Windows NT 5.1", 25)) {
  655. osWindowsXPCount.incrementAndGet();
  656. } else if (userAgent.startsWith("Windows NT 6.0", 13)) {
  657. osWindowsVistaCount.incrementAndGet();
  658. } else if (userAgent.startsWith("Windows NT 6.1", 13)) {
  659. osWindows7Count.incrementAndGet();
  660. } else if (userAgent.startsWith("Windows NT 6.2", 13)) {
  661. osWindows8Count.incrementAndGet();
  662. } else if (userAgent.startsWith("Windows NT 5.0", 13)) {
  663. osWindows2000Count.incrementAndGet();
  664. } else if (userAgent.startsWith("Windows NT 5.0", 25)) {
  665. osWindows2000Count.incrementAndGet();
  666. }
  667. }
  668. private void computeUserAgentIEWindowsVersion(String userAgent) {
  669. if (userAgent.startsWith("Windows NT 5.1", 35)) {
  670. osWindowsXPCount.incrementAndGet();
  671. } else if (userAgent.startsWith("Windows NT 5.0", 35)) {
  672. osWindows2000Count.incrementAndGet();
  673. } else if (userAgent.startsWith("Windows NT 5.0", 36)) {
  674. osWindows2000Count.incrementAndGet();
  675. } else if (userAgent.startsWith("Windows NT 6.0", 35)) {
  676. osWindowsVistaCount.incrementAndGet();
  677. } else if (userAgent.startsWith("Windows NT 6.1", 35)) {
  678. osWindows7Count.incrementAndGet();
  679. } else if (userAgent.startsWith("Windows NT 6.2", 36)) {
  680. osWindows8Count.incrementAndGet();
  681. } else if (userAgent.startsWith("Windows 98", 36)) {
  682. osWindows98Count.incrementAndGet();
  683. } else if (userAgent.startsWith("Windows 98", 35)) {
  684. osWindows98Count.incrementAndGet();
  685. } else if (userAgent.startsWith("Windows XP", 35)) {
  686. osWindowsXPCount.incrementAndGet();
  687. } else if (userAgent.startsWith("Windows XP", 34)) {
  688. osWindowsXPCount.incrementAndGet();
  689. }
  690. }
  691. private boolean computeUserAgentAndroid(String userAgent) {
  692. boolean isAndroid = userAgent.startsWith("Android", 23);
  693. int toffset = 31;
  694. if (!isAndroid) {
  695. isAndroid = userAgent.startsWith("Android", 20);
  696. toffset = 28;
  697. }
  698. if (isAndroid) {
  699. osAndroidCount.incrementAndGet();
  700. deviceAndroidCount.incrementAndGet();
  701. if (userAgent.startsWith("1.5", toffset)) {
  702. osAndroid15Count.incrementAndGet();
  703. } else if (userAgent.startsWith("1.6", toffset)) {
  704. osAndroid16Count.incrementAndGet();
  705. } else if (userAgent.startsWith("2.0", toffset)) {
  706. osAndroid20Count.incrementAndGet();
  707. } else if (userAgent.startsWith("2.1", toffset)) {
  708. osAndroid21Count.incrementAndGet();
  709. } else if (userAgent.startsWith("2.2", toffset)) {
  710. osAndroid22Count.incrementAndGet();
  711. } else if (userAgent.startsWith("2.3.3", toffset)) {
  712. osAndroid23Count.incrementAndGet();
  713. } else if (userAgent.startsWith("2.3.4", toffset)) {
  714. osAndroid23Count.incrementAndGet();
  715. } else if (userAgent.startsWith("3.0", toffset)) {
  716. osAndroid30Count.incrementAndGet();
  717. } else if (userAgent.startsWith("3.1", toffset)) {
  718. osAndroid31Count.incrementAndGet();
  719. } else if (userAgent.startsWith("3.2", toffset)) {
  720. osAndroid32Count.incrementAndGet();
  721. } else if (userAgent.startsWith("4.0", toffset)) {
  722. osAndroid40Count.incrementAndGet();
  723. } else if (userAgent.startsWith("4.1", toffset)) {
  724. osAndroid41Count.incrementAndGet();
  725. } else if (userAgent.startsWith("4.2", toffset)) {
  726. osAndroid42Count.incrementAndGet();
  727. } else if (userAgent.startsWith("4.3", toffset)) {
  728. osAndroid43Count.incrementAndGet();
  729. }
  730. return true;
  731. }
  732. return false;
  733. }
  734. public long getOSMacOSXCount() {
  735. return osMacOSXCount.get();
  736. }
  737. public long getOSWindowsCount() {
  738. return osWindowsCount.get();
  739. }
  740. public long getOSLinuxCount() {
  741. return osLinuxCount.get();
  742. }
  743. public long getOSSymbianCount() {
  744. return osSymbianCount.get();
  745. }
  746. public long getOSFreeBSDCount() {
  747. return osFreeBSDCount.get();
  748. }
  749. public long getOSOpenBSDCount() {
  750. return osOpenBSDCount.get();
  751. }
  752. public long getOSAndroidCount() {
  753. return osAndroidCount.get();
  754. }
  755. public long getOSWindows98Count() {
  756. return osWindows98Count.get();
  757. }
  758. public long getOSWindowsXPCount() {
  759. return osWindowsXPCount.get();
  760. }
  761. public long getOSWindows2000Count() {
  762. return osWindows2000Count.get();
  763. }
  764. public long getOSWindowsVistaCount() {
  765. return osWindowsVistaCount.get();
  766. }
  767. public long getOSWindows7Count() {
  768. return osWindows7Count.get();
  769. }
  770. public long getOSWindows8Count() {
  771. return osWindows8Count.get();
  772. }
  773. public long getOSAndroid15Count() {
  774. return osAndroid15Count.get();
  775. }
  776. public long getOSAndroid16Count() {
  777. return osAndroid16Count.get();
  778. }
  779. public long getOSAndroid20Count() {
  780. return osAndroid20Count.get();
  781. }
  782. public long getOSAndroid21Count() {
  783. return osAndroid21Count.get();
  784. }
  785. public long getOSAndroid22Count() {
  786. return osAndroid22Count.get();
  787. }
  788. public long getOSAndroid23Count() {
  789. return osAndroid23Count.get();
  790. }
  791. public long getOSAndroid30Count() {
  792. return osAndroid30Count.get();
  793. }
  794. public long getOSAndroid31Count() {
  795. return osAndroid31Count.get();
  796. }
  797. public long getOSAndroid32Count() {
  798. return osAndroid32Count.get();
  799. }
  800. public long getOSAndroid40Count() {
  801. return osAndroid40Count.get();
  802. }
  803. public long getOSAndroid41Count() {
  804. return osAndroid41Count.get();
  805. }
  806. public long getOSAndroid42Count() {
  807. return osAndroid42Count.get();
  808. }
  809. public long getOSAndroid43Count() {
  810. return osAndroid43Count.get();
  811. }
  812. public long getOSLinuxUbuntuCount() {
  813. return osLinuxUbuntuCount.get();
  814. }
  815. public long getBrowserIECount() {
  816. return browserIECount.get();
  817. }
  818. public long getBrowserFirefoxCount() {
  819. return browserFirefoxCount.get();
  820. }
  821. public long getBrowserChromeCount() {
  822. return browserChromeCount.get();
  823. }
  824. public long getBrowserSafariCount() {
  825. return browserSafariCount.get();
  826. }
  827. public long getBrowserOperaCount() {
  828. return browserOperaCount.get();
  829. }
  830. public long getBrowserIE5Count() {
  831. return browserIE5Count.get();
  832. }
  833. public long getBrowserIE6Count() {
  834. return browserIE6Count.get();
  835. }
  836. public long getBrowserIE7Count() {
  837. return browserIE7Count.get();
  838. }
  839. public long getBrowserIE8Count() {
  840. return browserIE8Count.get();
  841. }
  842. public long getBrowserIE9Count() {
  843. return browserIE9Count.get();
  844. }
  845. public long getBrowserIE10Count() {
  846. return browserIE10Count.get();
  847. }
  848. public long getBrowser360SECount() {
  849. return browser360SECount.get();
  850. }
  851. public long getDeviceAndroidCount() {
  852. return deviceAndroidCount.get();
  853. }
  854. public long getDeviceIpadCount() {
  855. return deviceIpadCount.get();
  856. }
  857. public long getDeviceIphoneCount() {
  858. return deviceIphoneCount.get();
  859. }
  860. public long getDeviceWindowsPhoneCount() {
  861. return deviceWindowsPhoneCount.get();
  862. }
  863. public long getBotCount() {
  864. return botCount.get();
  865. }
  866. public long getBotBaiduCount() {
  867. return botBaiduCount.get();
  868. }
  869. public long getBotYoudaoCount() {
  870. return botYoudaoCount.get();
  871. }
  872. public long getBotGoogleCount() {
  873. return botGoogleCount.get();
  874. }
  875. public long getBotMsnCount() {
  876. return botMsnCount.get();
  877. }
  878. public long getBotBingCount() {
  879. return botBingCount.get();
  880. }
  881. public long getBotSosoCount() {
  882. return botSosoCount.get();
  883. }
  884. public long getBotSogouCount() {
  885. return botSogouCount.get();
  886. }
  887. public long getBotYahooCount() {
  888. return botYahooCount.get();
  889. }
  890. public WebAppStatValue getStatValue(boolean reset) {
  891. WebAppStatValue val = new WebAppStatValue();
  892. val.setContextPath(contextPath);
  893. val.setRunningCount(getRunningCount());
  894. val.concurrentMax = get(concurrentMax, reset);
  895. val.requestCount = get(requestCount, reset);
  896. val.sessionCount = get(sessionCount, reset);
  897. val.jdbcFetchRowCount = get(jdbcFetchRowCount, reset);
  898. val.jdbcUpdateCount = get(jdbcUpdateCount, reset);
  899. val.jdbcExecuteCount = get(jdbcExecuteCount, reset);
  900. val.jdbcExecuteTimeNano = get(jdbcExecuteTimeNano, reset);
  901. val.jdbcCommitCount = get(jdbcCommitCount, reset);
  902. val.jdbcRollbackCount = get(jdbcRollbackCount, reset);
  903. val.osMacOSXCount = get(osMacOSXCount, reset);
  904. val.osWindowsCount = get(osWindowsCount, reset);
  905. val.osLinuxCount = get(osLinuxCount, reset);
  906. val.osSymbianCount = get(osSymbianCount, reset);
  907. val.osFreeBSDCount = get(osFreeBSDCount, reset);
  908. val.osOpenBSDCount = get(osOpenBSDCount, reset);
  909. val.osAndroidCount = get(osAndroidCount, reset);
  910. val.osWindows98Count = get(osWindows98Count, reset);
  911. val.osWindowsXPCount = get(osWindowsXPCount, reset);
  912. val.osWindows2000Count = get(osWindows2000Count, reset);
  913. val.osWindowsVistaCount = get(osWindowsVistaCount, reset);
  914. val.osWindows7Count = get(osWindows7Count, reset);
  915. val.osWindows8Count = get(osWindows8Count, reset);
  916. val.osAndroid15Count = get(osAndroid15Count, reset);
  917. val.osAndroid16Count = get(osAndroid16Count, reset);
  918. val.osAndroid20Count = get(osAndroid20Count, reset);
  919. val.osAndroid21Count = get(osAndroid21Count, reset);
  920. val.osAndroid22Count = get(osAndroid22Count, reset);
  921. val.osAndroid23Count = get(osAndroid23Count, reset);
  922. val.osAndroid30Count = get(osAndroid30Count, reset);
  923. val.osAndroid31Count = get(osAndroid31Count, reset);
  924. val.osAndroid32Count = get(osAndroid32Count, reset);
  925. val.osAndroid40Count = get(osAndroid40Count, reset);
  926. val.osAndroid41Count = get(osAndroid41Count, reset);
  927. val.osAndroid42Count = get(osAndroid42Count, reset);
  928. val.osAndroid43Count = get(osAndroid43Count, reset);
  929. val.osLinuxUbuntuCount = get(osLinuxUbuntuCount, reset);
  930. val.browserIECount = get(browserIECount, reset);
  931. val.browserFirefoxCount = get(browserFirefoxCount, reset);
  932. val.browserChromeCount = get(browserChromeCount, reset);
  933. val.browserSafariCount = get(browserSafariCount, reset);
  934. val.browserOperaCount = get(browserOperaCount, reset);
  935. val.browserIE5Count = get(browserIE5Count, reset);
  936. val.browserIE6Count = get(browserIE6Count, reset);
  937. val.browserIE7Count = get(browserIE7Count, reset);
  938. val.browserIE8Count = get(browserIE8Count, reset);
  939. val.browserIE9Count = get(browserIE9Count, reset);
  940. val.browserIE10Count = get(browserIE10Count, reset);
  941. val.browser360SECount = get(browser360SECount, reset);
  942. val.deviceAndroidCount = get(deviceAndroidCount, reset);
  943. val.deviceIpadCount = get(deviceIpadCount, reset);
  944. val.deviceIphoneCount = get(deviceIphoneCount, reset);
  945. val.deviceWindowsPhoneCount = get(deviceWindowsPhoneCount, reset);
  946. val.botCount = get(botCount, reset);
  947. val.botBaiduCount = get(botBaiduCount, reset);
  948. val.botYoudaoCount = get(botYoudaoCount, reset);
  949. val.botGoogleCount = get(botGoogleCount, reset);
  950. val.botMsnCount = get(botMsnCount, reset);
  951. val.botBingCount = get(botBingCount, reset);
  952. val.botSosoCount = get(botSosoCount, reset);
  953. val.botSogouCount = get(botSogouCount, reset);
  954. val.botYahooCount = get(botYahooCount, reset);
  955. return val;
  956. }
  957. }