Исходный код вики CheckXWikiConfig

Редактировал(а) Dmitriy Rybakov 05.08.2025, 13:07

Скрыть последних авторов
Dmitriy Rybakov 1.1 1 = XWiki Configuration Check =
2
3 {{groovy}}
4
5 import com.xpn.xwiki.util.Util;
6 import java.text.DecimalFormat;
7 import java.lang.management.ManagementFactory;
8 import javax.management.MBeanServer;
9 import groovy.jmx.GroovyMBean;
10 import groovy.xml.XmlParser;
11
12 if( System.getProperty('catalina.base') ) {
13 appserverpath = System.getProperty('catalina.base')
14 } else {
15 // CONFIG: try to set the path to ypur application server if not found
16 appserverpath = ".."
17 }
18
19 def xwikiPath = ["/etc/xwiki/xwiki.cfg", "${appserverpath}/webapps${request.contextPath}/WEB-INF/xwiki.cfg", "/usr/local/xwiki/WEB-INF/xwiki.cfg", "${appserverpath}/webapps/ROOT/WEB-INF/xwiki.cfg", "${appserverpath}/webapps/xwiki/WEB-INF/xwiki.cfg"]
20
21 def tomcatPath = ["${appserverpath}/conf/server.xml", "/usr/local/tomcat/conf/server.xml", "/var/lib/tomcat8/conf/", "/var/lib/tomcat9/conf/", "/var/lib/tomcat/conf/"]
22
23 def xwikicfgpath = "/usr/local/xwiki/WEB-INF/xwiki.cfg"
24 def tomcatconfpath = "/usr/local/tomcat/conf/server.xml"
25
26 for (path in xwikiPath) {
27 if ((new File(path)).exists()) {
28 xwikicfgpath = path
29 break;
30 }
31 }
32
33 for (path in tomcatPath) {
34 if ((new File(path)).exists()) {
35 tomcatconfpath = path
36 break;
37 }
38 }
39
40 def wiki = xcontext.getContext().getWiki()
41
42 def readableSize(size) {
43 if(size <= 0) return "0";
44 def units = ["B", "KB", "MB", "GB", "TB" ];
45 int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
46 return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
47 }
48
49 def xwikicfgdir = new File(xwikicfgpath)
50 def xwikicfgFound = xwikicfgdir.exists();
51 def xwencodingActive = xwiki.getEncoding()
52 if (xwikicfgFound) {
53 def content = Util.getFileContent(xwikicfgdir)
54 def is = new ByteArrayInputStream(content.getBytes());
55 def props = new Properties();
56 props.load(is);
57 xwencoding = props.get("xwiki.encoding")
58 xwencryptkey = props.get("xwiki.authentication.encryptionKey")
59 } else {
60 xwencoding = 'null'
61 xwencryptkey = 'null'
62 }
63
64
65 def getEnvVars() {
66 Process p = null;
67 Properties envVars = new Properties();
68 Runtime r = Runtime.getRuntime();
69 String OS = System.getProperty("os.name").toLowerCase();
70 if (OS.indexOf("windows 9") > -1) {
71 p = r.exec( "command.com /c set" );
72 }
73 else if ( (OS.indexOf("nt") > -1)
74 || (OS.indexOf("windows 2000") > -1 )
75 || (OS.indexOf("windows xp") > -1)
76 || (OS.indexOf("windows") > -1) ) {
77 // thanks to JuanFran for the xp fix!
78 p = r.exec( "cmd.exe /c set" );
79 }
80 else {
81 // our last hope, we assume Unix (thanks to H. Ware for the fix)
82 p = r.exec( "env" );
83 }
84 BufferedReader br = new BufferedReader
85 ( new InputStreamReader( p.getInputStream() ) );
86 String line;
87 while( (line = br.readLine()) != null ) {
88 int idx = line.indexOf( '=' );
89 String key = line.substring( 0, idx );
90 String value = line.substring( idx+1 );
91 envVars.setProperty( key, value );
92 // System.out.println( key + " = " + value );
93 }
94 return envVars;
95 }
96
97 println "This script intends to show most important parameters for XWiki hosting. You can [[go directly to recommendations>>${doc}||anchor=HRecommendations]]"
98
99 println "== Installation Path =="
100
101 println "* XWiki configuration path: ${xwikicfgpath}"
102 println "* Tomcat configuration path: ${tomcatconfpath}"
103
104 if (wiki!=null){
105 println "== Memory Configuration =="
106
107 println "This lists the memory settings of the Java VM and the current memory usage. An XWiki instance should be installed with at least 1Gb. A large one with at least 2GB to 4GB, especially if the cache settings are boosted. If after *flushing the cache*, the current free memory is low and the current total close to the maximum the instance might be running out of memory."
108 println "* Max Memory: ${readableSize(wiki.maxMemory())}"
109 println "* Current Total: ${readableSize(wiki.totalMemory())}"
110 println "* Current Free: ${readableSize(wiki.freeMemory())}"
111
112
113 println "== Cache configuration =="
114
115 println "This lists the main cache settings. An XWiki instance is set by default to 500 for the main cache which is fine for a normal instance. A large one with at least 1000 to 3000. Java Memory needs to be increased to support a larger cache. This setting can be changed in xwiki.cfg with the property xwiki.store.cache.capacity"
116
117 MBeanServer server = ManagementFactory.getPlatformMBeanServer()
118 def mbean = null
119
120 try {
121 mbean = new GroovyMBean(server, 'org.xwiki.infinispan:type=Cache,name="xwiki.store.pagecache(local)",manager="xwiki",component=Configuration')
122 } catch (Exception e) {
123 mbean = new GroovyMBean(server, 'org.xwiki.infinispan:type=Cache,name="xwiki.store.pagecache(local)",manager="XWikiCacheManager",component=Configuration')
124 }
125 esize = (mbean==null) ? null : mbean.getProperty("evictionSize")
126 csize = esize
127
128 try {
129 mbean = new GroovyMBean(server, 'org.xwiki.infinispan:type=Cache,name="xwiki.store.pagecache(local)",manager="xwiki",component=Statistics')
130 } catch (Exception e) {
131 mbean = new GroovyMBean(server, 'org.xwiki.infinispan:type=Cache,name="xwiki.store.pagecache(local)",manager="XWikiCacheManager",component=Statistics')
132 }
133 isize = (mbean==null) ? null : mbean.getProperty("numberOfEntries")
134 println "* Store Page Cache in Use : ${isize}/${esize}"
135
136
137
138 println "== Database configuration =="
139
140 println "This lists the database settings of the Java VM and the current pool usage. An XWiki instance is set by default to 2 connections in the pool, which is ok for a very small instance. A large one with at least 10 or 20. If there is no activity on the server, the active setting should be zero."
141
142
143 try {
144 def sessionFactory = wiki.getStore().getStore().sessionFactory
145 def provider = sessionFactory.@serviceRegistry.getService(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)
146 def ds = wiki.getPrivateField(provider,"ds")
147 def pool = wiki.getPrivateField(ds,"connectionPool")
148
149 def maxTotal
150 try { maxTotal = pool.maxTotal } catch (Exception e) { maxTotal = pool.maxActive }
151 println "* Max Active (number of possible connections to the DB): $maxTotal"
152 println "* Max Idle (number of possible idle connections to the DB): $pool.maxIdle"
153 println "* Active (current active connections to the DB): $pool.numActive"
154 println "* Idle (current idle connections to the DB): $pool.numIdle"
155 } catch (e) {
156 println "Cannot get database info " + e.getMessage()
157 }
158
159 println "== Authenticator Configuration =="
160
161 println "* Authenticator class configured in xwiki.cfg: " + wiki.Param("xwiki.authentication.authclass")
162
163 println "== Encoding Configuration =="
164
165 println "This lists ALL the encoding configuration of tomcat, xwiki, java. All should return UTF-8 except mail.mime.charset which can also return null (the file.encoding config will then be used)"
166
167
168 println "=== System environment ==="
169
170 println "* LANG :" + getEnvVars().get("LANG")
171 println "* PWD :" + getEnvVars().get("PWD")
172
173
174 println "=== Java command line ==="
175
176 def fileenc = System.getProperty("file.encoding")
177 println "* file.encoding: ${fileenc}"
178 def mimecharset = System.getProperty("mail.mime.charset")
179 println "* mail.mime.charset: ${mimecharset}"
180
181 def tomcatconffile = new File(tomcatconfpath)
182 if (tomcatconffile.exists()) {
183 def node = new XmlParser().parse(tomcatconffile)
184
185 println "=== Tomcat configuration ==="
186
187 println "Prior to Tomcat8 it was necessary to set the connection encoding of protocols to UTF-8. Since Tomcat8+ this is not necessary anymore."
188
189 for (connector in node.Service.Connector) {
190 urienc = connector.'@URIEncoding'
191 prot = connector.'@protocol'
192 println "* Procotol ${prot} has encoding ${urienc}"
193 }
194 } else {
195 println "* {{error}}tomcat.connectors.encoding : The tomcat configuration file could not be found. You might not be using tomcat or the app server path (appserverpath) variable need to be set (you can edit the page to set it){{/error}}"
196 }
197
198
199 println "=== XWiki Configuration ==="
200
201 println "* XWiki encoding used on server: ${xwencodingActive}"
202 if (xwikicfgFound) {
203 println "* XWiki encoding from config file: ${xwencoding}";
204 } else {
205 println "{{error}}The XWiki configuration file could not be found at ${xwikicfgpath}. The app server path (appserverpath) variable need to be set (you can edit the page to set it){{/error}}"
206 }
207
208 println "= Recommendations ="
209
210 println "Here will be shown our recommendations to increase wiki's performance and stability, if any :"
211
212 if (wiki.maxMemory() < 800000000) {
213 println "* {{warning}}Amount of memory allocated to XWiki is too low for a production usage. Max memory available for the JVM is set to ${readableSize(wiki.maxMemory())}, it should be at least 800MB.{{/warning}}"
214 }
215
216 if (csize < 1000) {
217 println "* {{warning}}Your cache configuration is too low for a production usage. Your cache is set to ${csize} documents, it should be 1000 or more.{{/warning}}"
218 }
219
220 if (xwencodingActive && xwencodingActive != "UTF-8") {
221 println "* {{warning}}XWiki encoding is not set properly. We advise to use UTF-8 encoding for every software used (Servlet container, front end, database...){{/warning}}"
222 }
223
224 if (xwencryptkey == "titititititititititititititititi") {
225 println "* {{warning}}You should change your encryption key (xwiki.authentication.encryptionKey) and validation key (xwiki.authentication.validationKey) of cookies in xwiki.cfg configuration file {{/warning}}"
226 }
227
228 }
229 {{/groovy}}