示例#1
0
        public override void Install(IDictionary stateSaver)
        {
            base.Install (stateSaver);

            Daemon rd = new Daemon();

            string loc = rd.GetType().Assembly.Location;

            /*System.IO.Stream rs = rd.GetType().Assembly.GetManifestResourceStream("ACM.WTS.dll");
            System.IO.Stream st = System.IO.File.OpenWrite(System.Environment.SystemDirectory + "\\WTS.dll");
            while(rs.Position < rs.Length)
            {
                byte[] buffer = new byte[1024];
                int bytes = rs.Read(buffer, 0, 1024);
                st.Write(buffer, 0, bytes);
            }
            rs.Close();
            st.Close();*/

            System.IO.File.Copy(loc,System.Environment.SystemDirectory + "\\ruptime.exe",true);

            Process proc = Process.Start(System.Environment.SystemDirectory + "\\sc.exe", "config rwhod binPath= \"" + loc + " service\"");
            if(proc != null)
            {
                proc.WaitForExit();
                proc.Close();
            }
            try
            {
                ServiceController sc = new ServiceController("rwhod");
                sc.Start();
                sc.WaitForStatus(ServiceControllerStatus.Running);
            }
            catch(System.Exception){}
        }
示例#2
0
文件: rwho.cs 项目: mloar/rwho
        static void Main(string[] args)
        {
            Daemon rd = new Daemon();
              if(rd.GetType().Assembly.Location.EndsWith("ruptime.exe"))
              {
            if( args.Length > 0 && (args[0] == "/?" || args[0] == "--help"))
            {
              Console.Write(RUPTIME_USAGE);
              return;
            }

            bool all = false;
            string sort = "f";
            foreach(string arg in args)
            {
              string sortstart = sort;
              if(arg.IndexOf("a") > -1)
            all = true;
              if(arg.IndexOf("r") > -1)
            sort = sort.Replace("f","r");
              if(arg.IndexOf("l") > -1)
            sort += "l";
              else if(arg.IndexOf("u") > -1)
            sort += "u";
              else if(arg.IndexOf("t") > -1)
            sort += "t";
              if((sort == sortstart && all == false) || sort.Length > 2)
              {
            Console.WriteLine("Invalid option - " + arg);
            Console.WriteLine("");
            Console.Write(RUPTIME_USAGE);
            return;
              }
            }
            if(sort.Length < 2)
              sort += "h";

            int timestamp = (int)((DateTime.UtcNow.Ticks - UNIX_ZERO_TICKS)/TimeSpan.TicksPerSecond);

            try
            {
              XmlDocument cache = new XmlDocument();
              string path = System.Environment.SystemDirectory;
              path = path.Remove(path.LastIndexOf('\\'),path.Length-path.LastIndexOf('\\'));
              System.IO.FileStream st = new System.IO.FileStream(path + "\\TEMP\\rwhocache.xml",System.IO.FileMode.Open,
              System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite);
              XmlTextReader reader = new XmlTextReader(st);

              cache.Load(reader);

              int i = 0;
              string[] lines = new string[cache.FirstChild.ChildNodes.Count];
              string[] keys = new string[cache.FirstChild.ChildNodes.Count];
              foreach(XmlElement node in cache.FirstChild.ChildNodes)
              {
            int downtime = timestamp - int.Parse(node.GetAttribute("recvtime"));
            if(downtime < 11 * 60)
            {
              int uptime = int.Parse(node.GetAttribute("uptime"));
              TimeSpan ts = new TimeSpan(((long)(timestamp - uptime)) * TimeSpan.TicksPerSecond);

              lines[i] = node.GetAttribute("hostname").PadRight(12) + "  up" + string.Format("{0}+{1:00}:{2:00}",
                  ts.Days,ts.Hours,ts.Minutes).PadLeft(12);

              int j = 0;
              int users = 0;
              foreach(XmlElement user in node.ChildNodes)
              {

                TimeSpan idle = new TimeSpan(Int64.Parse(user.GetAttribute("idle")) * 10000000);
                if(all || (idle.Ticks < 36000000000))
                  users++;
                j++;
              }
              lines[i] += ",\t" + (users.ToString() + " users, ").PadRight(10);
              lines[i] +=  string.Format("load {0:0.00}, {1:0.00}, {2:0.00}",
                  double.Parse(node.GetAttribute("loadav0"))/100,double.Parse(node.GetAttribute("loadav1"))/100,
                  double.Parse(node.GetAttribute("loadav2"))/100);

              if(sort[1] == 'l')
                keys[i] = string.Format("load {0:0.00}, {1:0.00}, {2:0.00}",
                    double.Parse(node.GetAttribute("loadav0"))/100,double.Parse(node.GetAttribute("loadav1"))/100,
                    double.Parse(node.GetAttribute("loadav2"))/100);
              else if(sort[1] == 'u')
                keys[i] = users.ToString();
              else if(sort[1] == 't')
                keys[i] = string.Format("{0}+{1:00}:{2:00}",ts.Days,ts.Hours,ts.Minutes).PadLeft(12);
              else
                keys[i] = node.GetAttribute("hostname");

              i++;
            }
            else if(downtime < 4 * 24 * 60 * 60)
            {
              TimeSpan ts = new TimeSpan(
                  (timestamp - int.Parse(node.GetAttribute("recvtime"))) * TimeSpan.TicksPerSecond);
              lines[i] = node.GetAttribute("hostname").PadRight(12) + "down" + string.Format("{0}+{1:00}:{2:00}",
                  ts.Days,ts.Hours,ts.Minutes).PadLeft(12);

              if(sort[1] == 'l')
                keys[i] = string.Format("load {0:0.00}, {1:0.00}, {2:0.00}",-1L,-1L,-1L);
              else if(sort[1] == 'u')
                keys[i] = string.Format("{0}",-1L);
              else if(sort[1] == 't')
                keys[i] = string.Format("{0}+{1:00}:{2:00}",-1L,-1L,-1L).PadLeft(12);
              else
                keys[i] = node.GetAttribute("hostname");

              i++;
            }
              }
              string[] alllines = new string[i];
              string[] allkeys = new string[i];
              Array.Copy(lines,alllines,i);
              Array.Copy(keys,allkeys,i);
              Array.Sort(allkeys,alllines);
              if(sort[0] != 'r' && sort != "fh" || sort == "rh")
            Array.Reverse(alllines);
              Console.Write(string.Join("\r\n",alllines).Trim() + "\r\n");
            }
            catch(System.IO.FileNotFoundException)
            {
              Console.WriteLine("Data cache missing.  Is the rwho service installed/started?");
              Console.WriteLine("Try running 'rwho install'.");
            }
            catch(System.Xml.XmlException)
            {
              Console.WriteLine("Corrupt data cache.");
            }
            catch(System.Security.SecurityException)
            {
              Console.WriteLine("You do not have read access to the rwho data cache.");
            }
            catch(System.Exception e)
            {
              Console.WriteLine("Could not access the data cache: " + e.Message);
            }

            return;
              }
              if(args.Length > 0 && (args[0] == "service" || args[0] == "install" || args[0] == "uninstall"
            || args[0] == "isdrivingcar"))
              {
            string loc = rd.GetType().Assembly.Location;
            string maxver = "";

            foreach(string dir in System.IO.Directory.GetDirectories(
              System.Environment.SystemDirectory.Replace("\\system32","") + "\\Microsoft.NET\\Framework","v?.?.?????"))
            {
              if(maxver.Length == 0 || (maxver.CompareTo(dir) < 0))
            maxver = dir;
            }
            if(args[0] == "service")
            {
              System.ServiceProcess.ServiceBase[] ServicesToRun;
              ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Daemon() };
              System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            }
            else if(args[0] == "install")
            {
              Process proc = Process.Start(maxver + "\\installutil.exe", "/LogFile=\"\" \"" + loc + "\"");
              if(proc != null)
              {
            proc.WaitForExit();
            if(proc.ExitCode != 0)
            {
              Console.WriteLine(string.Format(
                    "InstallUtil returned an error code:{0}  The installation may have failed.",proc.ExitCode));
              Console.WriteLine("Do you have administrative privileges?");
            }
            proc.Close();
              }
            }
            else if(args[0] == "uninstall")
            {
              Process proc = Process.Start(maxver + "\\installutil.exe","/u /LogFile=\"\" " + "\"" + loc + "\"");
              if(proc != null)
              {
            proc.WaitForExit();
            if(proc.ExitCode != 0)
            {
              Console.WriteLine(string.Format(
                    "InstallUtil returned an error code:{0}  The uninstallation may have failed.",proc.ExitCode));
              Console.WriteLine("Do you have administrative privileges?");
            }
            proc.Close();
              }
            }
            else
            {
              Console.WriteLine("rwho for Windows v" + rd.GetType().Assembly.GetName().Version.ToString());
              Console.WriteLine("Copyright (C) 2005-2006 ACM@UIUC.  Copying permitted under the terms of");
              Console.WriteLine("the University of Illinois-NCSA Open Source License.");
              Console.WriteLine("");
              Console.WriteLine("Written by Matthew Loar, the awesomest C# hacker EVAR!");
            }
              }
              else
              {
            bool all = false;
            if(args.Length > 0)
            {
              if(args[0] == "-a")
            all = true;
              else if(args[0] == "/a")
            all = true;
              else
              {
            Console.WriteLine("Invalid option - " + args[0]);
            Console.WriteLine("");
            Console.Write(RWHO_USAGE);
            return;
              }
            }

            int timestamp = (int)((DateTime.UtcNow.Ticks - UNIX_ZERO_TICKS)/TimeSpan.TicksPerSecond);

            try
            {
              XmlDocument cache = new XmlDocument();
              string path = System.Environment.SystemDirectory;
              path = path.Remove(path.LastIndexOf('\\'),path.Length-path.LastIndexOf('\\'));
              System.IO.FileStream st = new System.IO.FileStream(path + "\\TEMP\\rwhocache.xml",System.IO.FileMode.Open,
              System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite);
              XmlTextReader reader = new XmlTextReader(st);
              cache.Load(reader);

              string[] alllines = new string[0];
              foreach(XmlElement node in cache.DocumentElement.ChildNodes)
              {
            // Drop records older than 11 minutes
            if(timestamp - int.Parse(node.GetAttribute("recvtime")) < 660)
            {
              int i = 0;
              string[] lines = new string[node.ChildNodes.Count];
              foreach(XmlElement user in node.ChildNodes)
              {
                TimeSpan idle = new TimeSpan(Int64.Parse(user.GetAttribute("idle")) * 10000000);
                if(all || (idle.Ticks > 0 && idle.Ticks < 36000000000))
                  lines[i] = String.Format("{0}\t{1}\t{2}\t{3}",user.GetAttribute("name").PadRight(8),
                      (node.GetAttribute("hostname") + ":"+ user.GetAttribute("line")).PadRight(16),
                      new DateTime(Int64.Parse(user.GetAttribute("time"))
                        * 10000000 + UNIX_ZERO_TICKS).ToLocalTime().ToString("MMM dd HH:mm"),
                      (idle.Ticks>0)?String.Format("{0:00}:{1:00}",Math.Floor(idle.TotalHours),idle.Minutes):"");
                else if(Int64.Parse(user.GetAttribute("idle")) == 0)
                  lines[i] = String.Format("{0}\t{1}\t{2}",user.GetAttribute("name").PadRight(8),
                      (node.GetAttribute("hostname") + ":"+ user.GetAttribute("line")).PadRight(16),
                      new DateTime(Int64.Parse(user.GetAttribute("time"))
                        * 10000000 + UNIX_ZERO_TICKS).ToLocalTime().ToString("MMM dd HH:mm"));
                i++;
              }

              string[] temp = new string[lines.Length + alllines.Length];
              Array.Copy(alllines,0,temp,0,alllines.Length);
              Array.Copy(lines,0,temp,alllines.Length,lines.Length);
              alllines = temp;
            }
              }
              Array.Sort(alllines);
              foreach(string line in alllines)
              {
            if(line != null && line.Length != 0)
              Console.WriteLine(line);
              }
            }
            catch(System.IO.FileNotFoundException)
            {
              Console.WriteLine("Data cache missing.  Is the rwho service installed/started?");
              Console.WriteLine("Try running 'rwho install'.");
            }
            catch(System.Xml.XmlException)
            {
              Console.WriteLine("Corrupt data cache.");
            }
            catch(System.Security.SecurityException)
            {
              Console.WriteLine("You do not have read access to the rwho data cache.");
            }
            catch(System.Exception e)
            {
              Console.WriteLine("Could not access data cache: " + e.Message);
            }
              }
        }