]> granicus.if.org Git - icinga2/blob - agent/windows-setup-agent/Program.cs
Merge pull request #6561 from miso231/feature_itl_ceph
[icinga2] / agent / windows-setup-agent / Program.cs
1 using System;
2 using System.IO;
3 using System.Windows.Forms;
4 using Microsoft.Win32;
5 using System.Runtime.InteropServices;
6 using System.Text;
7
8 namespace Icinga
9 {
10     internal static class NativeMethods
11     {
12         [DllImport("msi.dll", CharSet = CharSet.Unicode)]
13         internal static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
14
15         [DllImport("msi.dll", CharSet = CharSet.Unicode)]
16         internal static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
17     }
18
19     static class Program
20         {
21                 public static string Icinga2InstallDir
22                 {
23                         get
24                         {
25                                 StringBuilder szProduct;
26
27                                 for (int index = 0; ; index++) {
28                                         szProduct = new StringBuilder(39);
29                                         if (NativeMethods.MsiEnumProducts(index, szProduct) != 0)
30                                                 break;
31
32                                         int cbName = 128;
33                                         StringBuilder szName = new StringBuilder(cbName);
34
35                                         if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
36                                                 continue;
37
38                                         if (szName.ToString() != "Icinga 2")
39                                                 continue;
40
41                                         int cbLocation = 1024;
42                                         StringBuilder szLocation = new StringBuilder(cbLocation);
43                                         if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
44                                                 return szLocation.ToString();
45                                 }
46
47                                 return "";
48                         }
49                 }
50
51                 public static string Icinga2DataDir
52                 {
53                         get
54                         {
55                                 return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\icinga2";
56                         }
57                 }
58
59                 public static string Icinga2User
60                 {
61                         get
62                         {
63                                 if (!File.Exists(Icinga2DataDir + "\\etc\\icinga2\\user"))
64                                         return "NT AUTHORITY\\NetworkService";
65                                 System.IO.StreamReader file = new System.IO.StreamReader(Icinga2DataDir + "\\etc\\icinga2\\user");
66                                 string line = file.ReadLine();
67                                 file.Close();
68
69                                 if (line != null)
70                                         return line;
71                                 else
72                                         return "NT AUTHORITY\\NetworkService";
73                         }
74                 }
75
76
77                 public static void FatalError(Form owner, string message)
78                 {
79                         MessageBox.Show(owner, message, "Icinga 2 Setup Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
80                         Application.Exit();
81                 }
82
83                 /// <summary>
84                 /// The main entry point for the application.
85                 /// </summary>
86                 [STAThread]
87                 static void Main()
88                 {
89                         Application.EnableVisualStyles();
90                         Application.SetCompatibleTextRenderingDefault(false);
91
92                         string installDir = Program.Icinga2InstallDir;
93
94                         if (installDir == "") {
95                                 FatalError(null, "Icinga 2 does not seem to be installed properly.");
96                                 return;
97                         }
98
99                         Form form;
100
101                         if (File.Exists(Program.Icinga2DataDir + "\\etc\\icinga2\\features-enabled\\api.conf"))
102                                 form = new ServiceStatus();
103                         else
104                                 form = new SetupWizard();
105
106                         Application.Run(form);
107                 }
108         }
109 }